Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

SWT Creates Fast, Native-looking GUIs for Your Java Apps : Page 2

Learn how the open source Standard Widget Toolkit (SWT) enables client-side Java applications to assume the interfaces and performance of traditional native desktop applications. Then use it to build highly responsive, GUI-rich Java applications.


advertisement

WEBINAR: On-Demand

Unleash Your DevOps Strategy by Synchronizing Application and Database Changes REGISTER >

SWT Components
Label
A label is a collection of characters that the user cannot modify (although they can be modified programmatically). Unlike Swing label or AWT label, SWT labels provide a special SEPARATOR style. The separator style is a special kind of label that draws a line separating different widgets. The following command places a label on the shell:

Label label1 = new Label(shell,SWT.BORDER);

Button
A button is a widget that can capture user clicks and initiate some processing. A SWT button provides a type field that can be used to distinguish among various types of buttons: PUSH, CHECK, RADIO, TOGGLE, and ARROW. In Swing, different components are provided (JCheckBox, JRadioButton, and JToggleButton) for each type of button. The following command places a Button widget on the shell:

Button button1 = new Button(shell,SWT.PUSH);

Text
A Text widget contains text that users can enter and modify. Unlike Swing's JTextField, you can set the max number of characters that the users can edit. To provide password-like services, Swing provides a separate class (JPasswordField). SWT offers the same function with the setEchoChar class in its Text widget. The following command places new Text widget on the shell:


Text text = new Text(shell, SWT.BORDER);

Combo
A combo widget allows the user to select an item from a collection of items. The user may also type a value into the combo widget. Since this widget is a composite containing Text and List, it contains the methods of both Text and List widgets. Swing JComboBox is a combination of JTextField, Jbutton, and a drop-down JList. The following command places a Combo widget on the shell:

Combo combo = new Combo(shell, SWT.DROP_DOWN);

List
A list is a widget that contains a collection of items. It allows the user to select an item from the collection. A list may be single or multi select. The list widget supports scrolling directly, unlike Swing, which requires you to add the JList to a JscrollPane. The following command places a List widget on the shell:

List list = new List(shell,SWT.MULTI);

Composite
A composite is a widget that can contain other widgets. You place widgets inside a composite the same way you place widgets on a shell. The position of each widget inside a composite is relative to the composite, so if the composite is moved on the shell, the widgets inside the composite retain their relative positions. The following command creates a Composite widget:

Composite compObj = new Composite(shell, SWT.BORDER);

TabFolder
Using the TabFolder widget also demonstrates the use of composites. This widget provides the notebook UI metaphor. It allows the user to select a notebook page from a set of pages. TabItem widgets can be added only to the TabFolder.

The following command creates a TabFolder widget:

TabFolder tf = new TabFolder(shell, SWT.NONE) ;

TabItem
TabItem is a selectable user interface object corresponding to a tab for a page in a tab folder. The following command creates a TabItem widget:

TabItem ti = new TabItem(tf, SWT.NO_FOCUS, 0) ; ti.setText("Tab 1");

To build the user interface that goes into the TabItem, use a composite widget:

Composite comp = new Composite(tf,SWT.NONE);

The item children that may be added to instances of this class must be of type TabItem.

Create Control children and then set them into a tab item using the setControl method.

Table
The table widget provides a selectable user interface that displays a list of images and strings and issues notification when selected. If the style is defined as CHECK, it creates a checkbox column as the first column:

Table table = new Table(shell,SWT.BORDER);

You can make the headers for the column visible with the following method:

table.setHeaderVisible(true);

You can make the lines visible on the table with the following method:

table.setLinesVisible(true);

By default, both these properties are set to false.

TableColumn represents a column in a table widget:

TableColumn name = new TableColumn(table1, SWT.LEFT);

TableItem represents a single row in the table. You can create a single row using the following command:

TableItem item1 = new TableItem(table1,SWT.NONE);

You can either create a single row or set data for one field at a time using setText(int, String).

Tree
Tree widget displays data in a hierarchical format:

Tree tree = new Tree(shell,SWT.MULTI | SWT.BORDER);

You can use TreeItem widgets to attach the items to the tree or recursively to attach new tree items to existing tree items. The following command creates a Tree widget and adds a child node to the tree:

TreeItem rootNode1 = new TreeItem(tree, SWT.NONE); rootNode1.setText("Root Node 1"); TreeItem childNode1 = new TreeItem(rootNode1, SWT.NONE); childNode1.setText("Child Node 1");

Menu and MenuItem
The MenuBar widget spans the top of the shell. To create a MenuBar use the following code:

Menu mBar = new Menu(shell,SWT.BAR); Shell.setMenuBar(menu);

Swing provides a separate component MenuBar, while the SWT menu provides a separate style SWT.BAR to create a menu bar. You also can use the Menu widget recursively to create sub-menus.

Use MenuItem widgets to create a selectable menu item from the menu. The main advantage of SWT widgets is they provide different styles to create different types of menu items.

To create a radio box use the style SWT.RADIO, to create a check menu item use SWT.CHECK. SWT Menu provides other aspects such as accelerators, mnemonics.

Running an Application
The simple application included in the code download demonstrates the usage of each of the SWT widgets discussed in this article. Once you've unzipped the package, include swt.jar in your class path and compile the MyFirstSWTApp.java file.

Run the application with the following command:

Java –Djava.library.path=<Directory of dll> MyFirstSWTApp

This gets you started on using the SWT widgets and details the available basic widgets in SWT.



Raghu Donepudi, an independent contractor, currently is a technical manager for a federal government agency. He has a master's degree in Computer Science from Lamar University in Texas. He is a Sun-certified Java developer and the author of many software design techniques.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date