Reputation: 87
I have created a List
in a TabFolder
and for that list of elements I want to have a right click option for each elements of the list. So how can this be done in SWT.
I have added the elements to the list as follows:
list.add("a");
list.add("b");
list.add("b");
list.add("v");
list.add("d");
list.add("l");
Now how can I create a right click option for this list for each element?
Upvotes: 2
Views: 1090
Reputation: 36894
This should be a good starting point:
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new FillLayout());
final List list = new List(shell, SWT.BORDER);
list.add("a");
list.add("b");
list.add("b");
list.add("v");
list.add("d");
list.add("l");
final Menu menu = new Menu(list);
list.setMenu(menu);
menu.addMenuListener(new MenuAdapter()
{
public void menuShown(MenuEvent e)
{
int selected = list.getSelectionIndex();
if(selected < 0 || selected >= list.getItemCount())
return;
MenuItem[] items = menu.getItems();
for (int i = 0; i < items.length; i++)
{
items[i].dispose();
}
MenuItem newItem = new MenuItem(menu, SWT.NONE);
newItem.setText("Menu for \"" + list.getItem(list.getSelectionIndex()) + "\"");
}
});
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
Looks like this:
Upvotes: 4