Trees
are used to display information in hierarchical format. You can get
notifications when an item in the hierarchy is selected.
Following
styles are used while creating Tree instance.
Style
|
Description
|
SINGLE
|
Style
constant for single selection behavior. You can able to select only one item
in the tree at a time.
|
MULTI
|
Style
constant for multi-selection behavior. You can able to select multiple items in the
tree.
|
CHECK
|
Style
constant for check box behavior.
|
FULL_SELECTION
|
Style
constant for full row selection behavior.
|
VIRTUAL
|
Style
constant to allow virtual data. By using this, you can populate tree items on
demand basis.
|
NO_SCROLL
|
Style
constant for no scrollbar behavior.
|
How to define a tree?
By
using Tree constructor, you can define a tree.
Tree
tree = new Tree(shell, SWT.MULTI | SWT.BORDER);
tree.setSize(150,
150);
tree.setLocation(5,
5);
How to add an item to
hierarchy in the tree?
By
using TreeItem class, you can add an item to tree hierarchy. TreeItem class
provides following constructors to define a TreeItem instance.
Following
two constructors are used to attach TreeItem to a tree.
public
TreeItem (Tree parent, int style)
public
TreeItem (Tree parent, int style, int index)
Following
two constructors are used to attach a TreeItem to another TreeItem.
public
TreeItem (TreeItem parentItem, int style)
public
TreeItem (TreeItem parentItem, int style, int index)
You
can also add an image to TreeItem. Following statements adds an image to
TreeItem.
Image
icon = new Image(display, "help.bmp");
TreeItem
item = new TreeItem(tree, SWT.NONE);
item.setImage(icon);
By
using SelectionListener, you can able to get all the TreeItems selected in the
tree.
tree.addSelectionListener(new
SelectionAdapter() {
public void
widgetSelected(SelectionEvent e) {
TreeItem[] selectedItems =
tree.getSelection();
System.out.print("Selected
Items: ");
for (int i = 0; i <
selectedItems.length; i++) {
System.out.print(selectedItems[i].getText()
+ ", ");
}
System.out.println();
}
});
Get the TreeItem
expansion and collapsed status
By
using TreeListener interface, you can get notification, whenever the tree item
is collapsed (or) expanded.
TreeListener
interface provides following methods.
Method
|
Description
|
public
void treeCollapsed(TreeEvent e)
|
Notify
when a tree branch is collapsed.
|
public
void treeExpanded(TreeEvent e)
|
Notify
when a tree branch is expanded.
|
Following
is the complete working application.
package swt_app; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.TreeEvent; import org.eclipse.swt.events.TreeListener; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Tree; import org.eclipse.swt.widgets.TreeItem; public class Test { private static int shellWidth = 1000; private static int shellHeight = 700; private static void addWidgetsToShell(Display display, Shell shell) { Tree tree = new Tree(shell, SWT.MULTI | SWT.BORDER); tree.setSize(150, 150); tree.setLocation(5, 5); TreeItem child1 = new TreeItem(tree, SWT.NONE); child1.setText("child1"); TreeItem child3 = new TreeItem(tree, SWT.NONE); child3.setText("child3"); TreeItem child2 = new TreeItem(tree, SWT.NONE, 1); child2.setText("child2"); TreeItem child2_1 = new TreeItem(child2, SWT.NONE); child2_1.setText("child2_1"); TreeItem child2_2 = new TreeItem(child2, SWT.NONE); child2_2.setText("child2_2"); tree.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { TreeItem[] selectedItems = tree.getSelection(); System.out.print("Selected Items: "); for (int i = 0; i < selectedItems.length; i++) { System.out.print(selectedItems[i].getText() + ", "); } System.out.println(); } }); tree.addTreeListener(new TreeListener() { public void treeCollapsed(TreeEvent e) { System.out.println("Tree collapsed."); } public void treeExpanded(TreeEvent e) { System.out.println("Tree expanded."); } }); } public static void main(String[] args) { /* Instantiate Display object, it represents SWT session */ Display display = new Display(); /* * Define Shell, it represent a window, You can add more than one shell * to Display */ Shell shell = new Shell(display); shell.setSize(shellWidth, shellHeight); shell.setText("SWT Tutorial"); addWidgetsToShell(display, shell); /* Open shell window */ shell.open(); /* * Run the event dispatching loop until an exit condition occurs, which * is typically when the main shell window is closed by the user. */ while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } /* Dispose the display */ display.dispose(); } }
No comments:
Post a Comment