Forum Controls
Spotlight Features

The Rich Engineering Heritage Behind Dependency Injection

Andrew McVeigh takes us on a tour of the rich heritage behind dependency injection, what it represents, and tells us why its here to stay.

NetBeans 6: Matisse Updates

NetBeans 6 delivers great updates to the Matisse GUI builder. Spend a few minutes with Roman Strobl and get an expert briefing on what's new and what has changed.

Introduction to Groovy Part 3

In this, the third and final installation of Andres' Introduction to Groovy series, you learn about how Groovy handles variable numbers of arguments, named parameters, currying, and more about Groovy operators. Including, some new operators.

Easier Custom Components with Swing Fuse

Swing Fuse (actually just Fuse), is a framework designed to make it easier to create your own custom desktop components. In this article, Daniel Spiewak shows you how to get started and provides sample source code you can download.

Benchmark Analysis: Guice vs Spring

Willam Louth shows how he uses JXInsight Probes to investigate probable performance issues with code bases that he is not familiar with. He also highlights possible pitfalls in creating a benchmark, as well as in the analysis of results.
Replies: 12 - Pages: 1  
Threads: [ Previous | Next ]
  Click to reply to this thread Reply

SWT: Fancy Tabs

At 2:05 PM on Dec 29, 2004, R.J. Lorimer wrote:

SWT has a widget set for handling tabs: org.eclipse.swt.widgets.TabFolder . For those of you familiar with Swing, TabFolder is the SWT counterpart of javax.swing.TabbedPane . Creating a tab folder is relatively easy - here is an example code block from the SWT snippets:

Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
 
TabFolder tabFolder = new TabFolder (shell2, SWT.NONE);
for (int i=0; i<6; i++) {
 TabItem item = new TabItem (tabFolder, SWT.NULL);
 item.setText ("Tab Item " + i);
 Text text = new Text(tabFolder, SWT.BORDER | SWT.MULTI);
 text.setText("Content for Item "+i);
 item.setControl(text);
}
tabFolder.setSize (400, 200);
 
shell.pack ();
shell.open ();
while (!shell.isDisposed()) {
 if (!display.readAndDispatch()) 
  display.sleep();
}
display.dispose();




The default tab folder has its limitations, however. Because it uses a native 'tab' widget, it is limited to what each native platform is capable of. That averages out to the bare minimum required of tab widgets. That means no tabs on the bottom of the pane (you can orient the tabs on the bottom, but they won't look right on most platforms), no buttons or controls on the tab itself, and no extra graphical options for the tab. Those of you who have used Eclipse or Azureus (and are aware both are written using SWT) are probably aware that there is a way to achieve all of these things with tabs through SWT.

The org.eclipse.swt.custom.CTabFolder ('custom' tab folder) is a special widget that models a traditional widget (as are all of the org.eclipse.swt.custom.* widgets), but adds functionality not available on the default platform.

Usage of the CTabFolder should look very familiar to the regular tab folder:

Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
 
CTabFolder tabFolder = new CTabFolder(shell, SWT.NONE);
 
for (int i = 0; i < 6; i++) {
 CTabItem item = new CTabItem(tabFolder, SWT.NONE);
 item.setText("Tab Item "+i);
 Text text = new Text(folder, SWT.BORDER | SWT.MULTI);
 text.setText("Content for Item "+i);
 item.setControl(text);
}
 
tabFolder.setSize(400, 200);
 
shell.pack ();
shell.open ();
while (!shell.isDisposed()) {
 if (!display.readAndDispatch()) 
  display.sleep();
}
display.dispose();


In fact, this code is identical with the exception of the construction of the CTabFolder and the CTabItem classes themselves.



So, how can you take advantage of the 'custom' features? To add a close button to the tab items, simply construct the CTabFolder with the SWT.CLOSE constant:

CTabFolder tabFolder = new CTabFolder(shell, SWT.CLOSE);
// optional setting - enables close buttons only on selected tab (defaults to true)
tabFolder.setUnselectedCloseVisible(false);




To change the orientation of the tabs (from top to bottom), simply construct the tab folder with the SWT.BOTTOM constant:

CTabFolder tabFolder = new CTabFolder(shell, SWT.CLOSE | SWT.BOTTOM);




Finally, to give the tabs that nifty 'swoosh' that can be seen in Eclipse, set the 'simple' property on the folder to false:

tabFolder.setSimple(false);




Here is a comprehensive test class:

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;
 
public class TabsTest {
 
	public static void main(String[] args) {
		
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setLayout(new FillLayout());
	
		Shell shell2 = new Shell(display);
		shell2.setLayout(new FillLayout());
		CTabFolder folder = new CTabFolder(shell, SWT.CLOSE | SWT.BOTTOM);
		folder.setUnselectedCloseVisible(false);
		folder.setSimple(false);
		for (int i = 0; i < 6; i++) {
			CTabItem item = new CTabItem(folder, SWT.NONE);
			item.setText("Tab Item "+i);
			Text text = new Text(folder, SWT.BORDER | SWT.MULTI);
			text.setText("Content for Item "+i);
			item.setControl(text);
		}
		
		folder.setSize (400, 200);
		
		
		TabFolder tabFolder = new TabFolder (shell2, SWT.NONE);
		for (int i=0; i<6; i++) {
			TabItem item = new TabItem (tabFolder, SWT.NULL);
			item.setText ("Tab Item " + i);
			Text text = new Text(tabFolder, SWT.BORDER | SWT.MULTI);
			text.setText("Content for Item "+i);
			item.setControl(text);
		}
		tabFolder.setSize (400, 200);
		
		shell.pack ();
		shell.open ();
		shell2.pack();
		shell2.open();
		while (!shell.isDisposed () || !shell2.isDisposed()) {
			if (!display.readAndDispatch ()) display.sleep ();
		}
		display.dispose ();
		
	}
}


A couple other things to mention - the core TabFolder provides scroll buttons for navigating through tabs when there isn't enough room display each tab. The CTabFolder brings the custom chevron with it:

1 . At 4:59 PM on Jan 2, 2005, Matt Casters DeveloperZone Top 100 wrote:
  Click to reply to this thread Reply

Re: SWT: Fancy Tabs

Thank you! This was very useful for me.
I rewrote the TabFolders in my applications using your examples.

Just one remark. I found that using

tabFolder.setSelection(0);

is required to select the first tab. This is done by default in the standard TabFolder widget.

All the best,

Matt
Matt Casters on Data Integration and Kettle
2 . At 7:34 AM on Sep 6, 2005, Harsha Dinendra wrote:
  Click to reply to this thread Reply

Re: SWT: Fancy Tabs

Hi,
This was interesting, I just want to know how to add shading effects to tabfolder.
Thanks
-Harsha
3 . At 4:55 AM on Dec 14, 2005, Gireesh kumar wrote:
  Click to reply to this thread Reply

Re: SWT: Fancy Tabs

Hi

I wanted to know how can I arrange tabs like Yahoo calendar tabs. I also wanted to apply my own styles

Thanks
Giri
Gireesh Kumar Engg. Lead LightSurf Technologies (India)
4 . At 2:07 PM on Mar 8, 2006, Bianca Franco wrote:
  Click to reply to this thread Reply

Re: SWT: Fancy Tabs

Hi, I´ve been trying to fit tabs in multi rows, just like System Properties in Windows Control Panel does, for instance. I´ve been using RowLayout and wrap properties, but I haven´t had success. I´m sending the code below. Could anyone help me? Thanks!

import org.eclipse.swt.*;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.*;


public class Snippet76 {

public static void main (String [] args) {
Display display = new Display ();
final Shell shell = new Shell (display);
final TabFolder tabFolder = new TabFolder (shell, SWT.BORDER);

RowLayout rowLayout = new RowLayout();
rowLayout.wrap=true;
rowLayout.pack=false;
rowLayout.type = SWT.WRAP;

tabFolder.setLayout(rowLayout);


for (int i=0; i TabItem item = new TabItem (tabFolder, SWT.NONE);
item.setText ("TabItem " + i);
Button button = new Button (tabFolder, SWT.PUSH);
button.setText ("Page " + i);
item.setControl (button);
}
tabFolder.setSize (400, 200);
shell.pack ();
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch()) display.sleep ();
}
display.dispose ();
}
}
5 . At 3:09 AM on Jun 22, 2006, Chetan Shah wrote:
  Click to reply to this thread Reply

Re: SWT: Fancy Tabs

Hello,

I have not been ablt to figure out how to detect a click even on a CTabItem. I want to set different selectionProviders depending on the tab that is clicked.

I didn't see a listener that listens to the click events. May be there is a round about way or could be that I am missing something.

Thanks,
CS
6 . At 2:56 AM on Dec 11, 2006, Teena wrote:
  Click to reply to this thread Reply

Re: SWT: Fancy Tabs

Hello,

I need to add a listener to the TabItems, so that i can call the functionality based on the tab which is being displayed.


I dont know to add the listener to the TabItem. Can you please send me a sample code or any link that can help to do that.

With regards,
Teena
7 . At 3:03 AM on Dec 11, 2006, Teena wrote:
  Click to reply to this thread Reply

Re: SWT: Fancy Tabs

Hello,

I need to add a listener to the TabItems, so that i can call the functionality based on the tab which is being displayed.


I dont know to add the listener to the TabItem. Can you please send me a sample code or any link that can help to do that.

With regards,
Teena
8 . At 11:25 AM on Dec 24, 2006, Vyas wrote:
  Click to reply to this thread Reply

Re: SWT: Fancy Tabs

Java gaining in GUI!
Bioinformatics, Operating Systems and Computer Networks articles
9 . At 6:05 AM on Mar 19, 2007, Israr Ahmed wrote:
  Click to reply to this thread Reply

Re: SWT: Fancy Tabs

Thanks i tried it but tell me how we can give 3D effect to tabs.
There certainly have been performance issues with Java. We've been working really hard on them. The primary way we've attacked the problem is with advanced virtual machines. The performance has been getting very nice. --James Gosling, 1999.
10 . At 9:57 AM on Apr 16, 2007, clauidu iacob wrote:
  Click to reply to this thread Reply

Re: SWT: Fancy Tabs

Hi all,
Many of you have asked on how to achieve the 3D effect, as in Eclipse. Here is an example, taken from the SWT API itself:


setSelectionBackground

public void setSelectionBackground(Color[] colors,
int[] percents)

Specify a gradient of colours to be draw in the background of the selected tab. For example to draw a gradient that varies from dark blue to blue and then to white, use the following call to setBackground:

cfolder.setBackground(new Color[]{display.getSystemColor(SWT.COLOR_DARK_BLUE),
display.getSystemColor(SWT.COLOR_BLUE),
display.getSystemColor(SWT.COLOR_WHITE),
display.getSystemColor(SWT.COLOR_WHITE)},
new int[] {25, 50, 100});


Parameters:
colors - an array of Color that specifies the colors to appear in the gradient in order of appearance left to right. The value null clears the background gradient. The value null can be used inside the array of Color to specify the background color.
percents - an array of integers between 0 and 100 specifying the percent of the width of the widget at which the color should change. The size of the percents array must be one less than the size of the colors array.
11 . At 4:16 AM on Dec 22, 2007, Stan wrote:
  Click to reply to this thread Reply

Re: SWT: Fancy Tabs

R.J. --

After finding this article in dire need, it occurred to me that your articles have saved me on more than one occasion with information that is simply no where else to be found in the same readable form (or found at all). Right now its SWT, but before it was Hibernate, and the list goes on.

So I wanted to say thank you so very much for being as informative as you are prolific and insightful.

It takes a lot of skill to see both the forest and trees... but to be able to communicate both as well? My hat is off to you. Thanks for being such a valuable teacher in the online community.
12 . At 8:48 AM on Jan 27, 2008, patence wrote:
  Click to reply to this thread Reply

Re: SWT: Fancy Tabs

The toolbar is so cool
Thanks for sharing.
Java Software

thread.rss_message