Tree & TreeItems with SWT.SELECTION [message #453851] |
Tue, 12 April 2005 06:49  |
Eclipse User |
|
|
|
Hi all,
IŽm using a Tree to represent graphically a query. The query can be
modified by using different popup menus appended to the TreeItems. Under
Windows XP it works fine, but under linux unfortunately not. There is a
problem with the event I'm using:
- under Windows whenever I right-click on a TreeItem, it implies that the
TreeItem gets the Selection and therefore the correct popup-menu appears;
- under Linux when I right-click on a TreeItem, the TreeItem DOES NOT get
the
Selection and therefore a wrong popup-menu appears. Then, the user has
to,
first left-click on a TreeItem to select it and second right-click on it
to
get the correct popup-menu.
IŽm using the event MouseDown. Is there an event that can produce the same
(correct) behaviour under every platform? Have you understood the problem?
Does anybody have this problem?
Thanks,
Ivan
|
|
|
|
|
|
Re: Tree & TreeItems with SWT.SELECTION [message #453949 is a reply to message #453914] |
Thu, 14 April 2005 13:08   |
Eclipse User |
|
|
|
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.27392 seconds