Reputation: 13
So I have a menu for a table item and I try to add a MenuListener for it. While debugging, on first click on the table, only the listener is created and the event is not handled. On the second click, it behaves as it should. I have the following code:
private void createWidgets(Composite composite)
{
//some tabs are created
for (int i = 0; i < numberOfTabs; i++)
{
//tabs are populated with tables
this.tables[i].getTableViewer().getTable().addMenuDetectListener(new MenuDetectListener() {
@Override
public void menuDetected(MenuDetectEvent e) {
Table table = (Table) e.getSource();
addMenuForTable(table);
}
});
}
this.tabFolder.setSelection(0);
}
private void addMenuForTable(Table table)
{
final Menu someMenu = new Menu(table);
table.setMenu(someMenu);
someMenu.addMenuListener(new ChangingMenu(this, table, someMenu));
}
And my custom ChangingMenu class looks like this
public class ChangingMenu extends MenuAdapter {
private Menu someMenu;
private SomeView view;
private Table table;
public ChangingMenu(SomeView view, Table viewer, Menu someMenu) {
this.view = view;
this.someMenu= someMenu;
this.table = viewer;
}
@Override
public void menuShown(MenuEvent e) {
MenuItem[] menuItems = this.logChangeMenu.getItems();
for (int i = 0; i < menuItems.length; i++) {
menuItems[i].dispose();
}
addMenu(this.table);
}
private void addMenu(Table table) {
MenuItem menuItem= new MenuItem(this.someMenu, SWT.NONE);
menuItem.setText("Some text here");
//here I have a listener for this menu item that opens some dialog on item click
}
}
So on fist click on table, the Changing Menu is created, but the showMenu is not called somehow. Only on second click I get to see the menu item declared there.
Upvotes: 1
Views: 51
Reputation: 111142
Adding the menu in a menu detect listener is too late, there is no need to use this listener here. Just add the menu as soon as the table has been created:
addMenuForTable(this.tables[i].getTableViewer().getTable());
Upvotes: 1