|
|
|
|
Re: Tree & TreeItems with SWT.SELECTION [message #453949 is a reply to message #453914] |
Thu, 14 April 2005 17:08 |
Ivan Messages: 149 Registered: July 2009 |
Senior Member |
|
|
Thanks Steve! I have your book and it's really well done!
Here is a small snippet of the code that should have different results
depending on the OS:
public class TestTree {
public static void main (String [] args) {
Display display = new Display ();
final Shell shell = new Shell (display);
final Tree tree = new Tree (shell, SWT.BORDER | SWT.V_SCROLL |
SWT.H_SCROLL);
final Menu rootMenu = new Menu(shell, SWT.POP_UP);
MenuItem addChild = new MenuItem(rootMenu, SWT.PUSH);
addChild.setText("Add first Item");
final Menu childMenu = new Menu(shell, SWT.POP_UP);
MenuItem childMenuItem = new MenuItem(childMenu, SWT.PUSH);
childMenuItem.setText("delete Item");
final TreeItem rootItem = new TreeItem(tree, SWT.PUSH);
rootItem.setText("Root");
tree.setSize (100, 100);
tree.addListener (SWT.MouseDown, new Listener () {
public void handleEvent (Event event) {
if (tree.getTopItem().getItemCount() == 0) {
tree.setMenu(rootMenu);
} else if (tree.getTopItem().getItemCount() == 1) {
TreeItem[] itemSelected = tree.getSelection();
if (itemSelected[0].getText() == "Root") {
tree.setMenu(null);
} else
tree.setMenu(childMenu);
} else tree.setMenu(null);
}
});
addChild.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent arg0) {
TreeItem item = new TreeItem(rootItem, SWT.PUSH);
item.setText("Child");
}
public void widgetDefaultSelected(SelectionEvent arg0) {
// TODO Auto-generated method stub
}
});
childMenuItem.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent arg0) {
TreeItem[] item = tree.getSelection();
item[0].dispose();
}
public void widgetDefaultSelected(SelectionEvent arg0) {
// TODO Auto-generated method stub
}
});
shell.setSize (200, 200);
shell.open ();
while (!shell.isDisposed()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
Please give me some feedback.
Thanks in advance,
Ivan
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02667 seconds