| Author |
Message |
|
|
Post subject: Linking an SWT TreeItem with a Popup Menu
Posted: Aug 02, 2005 - 06:02 AM
|
|

Joined: Feb 23, 2005
Posts: 5
|
|
Hi I am trying to display a popup menu if a user would right click on a treeItem...
There is a mouse listener for a tree but this listener goes over the tree area rather then the specific nodes (treeItems) in the tree.
mytree.addMouseListener(new MouseAdapter(){
public void mouseDown(MouseEvent arg0) {
also there is no setMenu() method for the treeItem this is why I have been using mytree..
I want to be able to retrieve the treeItem data simlar to when an treeItem is selected... such as the below...
mytree.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
TreeItem[] t = rosterList.getSelection();
TreeItem item = (TreeItem) e.item;
Object o = item.getData();
can any one help?
Thanks
Austin |
|
|
| |
|
|
|
 |
|
|
Post subject:
Posted: Aug 02, 2005 - 08:25 AM
|
|
|
|
You can't add a popup menu to a TreeItem, but you can add a popup menu to the Tree widget itself and dynamically configure it based on the selection in the Tree.
You can either add/remove menu items or add all of them up front and then enable/disable them as needed.
Here's a simple example:
| Code: | import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MenuAdapter;
import org.eclipse.swt.events.MenuEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
public class ABC {
private Tree tree;
protected Shell shell;
public static void main(String[] args) {
try {
ABC window = new ABC();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
public void open() {
final Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
protected void createContents() {
shell = new Shell();
shell.setLayout(new FillLayout());
shell.setSize(300, 200);
shell.setText("Dynamic Menu Example");
tree = new Tree(shell, SWT.BORDER);
final TreeItem item1 = new TreeItem(tree, SWT.NONE);
item1.setText("First");
final TreeItem item2 = new TreeItem(tree, SWT.NONE);
item2.setText("Second");
final Menu menu = new Menu(tree);
tree.setMenu(menu);
menu.addMenuListener(new MenuAdapter() {
public void menuShown(MenuEvent e) {
// Get rid of existing menu items
MenuItem[] items = menu.getItems();
for (int i = 0; i < items.length; i++) {
((MenuItem) items[i]).dispose();
}
// Add menu items for current selection
MenuItem newItem = new MenuItem(menu, SWT.NONE);
newItem.setText("Menu for " + tree.getSelection()[0].getText());
}
});
}
} |
|
|
|
| |
|
|
|
 |
|
|
Post subject:
Posted: Aug 02, 2005 - 03:39 PM
|
|
|
|
| Do you know how to add a pop up menu to the TableTreeColumn? |
|
|
| |
|
|
|
 |
|
|
Post subject:
Posted: Aug 04, 2005 - 03:36 AM
|
|

Joined: Feb 23, 2005
Posts: 5
|
|
|
|
|
 |
|
|
|
Post subject: Thanks!!
Posted: Mar 10, 2008 - 10:54 AM
|
|
Joined: Mar 10, 2008
Posts: 1
|
|
Thanks!!  |
|
|
| |
|
|
|
 |
|
|
| |