0

我想在多个表(SWT 表)上添加相同的菜单。当用户单击菜单项时,我想检测与该菜单关联的表。这是下面的代码。

 private void setMenu(Table table) {
        Listener popUpListener = new Listener() {

            @Override
            public void handleEvent(Event event) {
                MenuItem item = (MenuItem)event.widget;
                Menu pare = item.getParent();
                Decorations fdf = pare.getParent();
                                    //Code to get table name or instance of table.

            }
        };
        Menu menu = new Menu(table); // where table1 is your table
        MenuItem item1 = new MenuItem(menu, SWT.PUSH);
        item1.setText("Copy cell");
        item1.addListener(SWT.Selection, popUpListener);
        MenuItem item2 = new MenuItem(menu, SWT.PUSH);
        item2.setText("Copy row");
        item2.addListener(SWT.Selection, popUpListener);
        MenuItem item3 = new MenuItem(menu, SWT.PUSH);
        item3.setText("Copy column");
        item3.addListener(SWT.Selection, popUpListener);
        MenuItem item4 = new MenuItem(menu, SWT.PUSH);
        item4.setText("Copy all");
        item4.addListener(SWT.Selection, popUpListener);
        table.setMenu(menu);
    }

多次调用 setMenu 方法发送表的实例。如何在弹出侦听器中检测与当前弹出菜单关联的表。

4

1 回答 1

0

只需将Table您发送到方法final

private void setMenu(final Table table) {
    Listener popUpListener = new Listener() {

        @Override
        public void handleEvent(Event event) {
            MenuItem item = (MenuItem)event.widget;
            Menu pare = item.getParent();
            Decorations fdf = pare.getParent();

            // Now you can access the table from within the Listener
            System.out.println(table);
        }
    };
    Menu menu = new Menu(table); // where table1 is your table
    MenuItem item1 = new MenuItem(menu, SWT.PUSH);
    item1.setText("Copy cell");
    item1.addListener(SWT.Selection, popUpListener);
    MenuItem item2 = new MenuItem(menu, SWT.PUSH);
    item2.setText("Copy row");
    item2.addListener(SWT.Selection, popUpListener);
    MenuItem item3 = new MenuItem(menu, SWT.PUSH);
    item3.setText("Copy column");
    item3.addListener(SWT.Selection, popUpListener);
    MenuItem item4 = new MenuItem(menu, SWT.PUSH);
    item4.setText("Copy all");
    item4.addListener(SWT.Selection, popUpListener);
    table.setMenu(menu);
}
于 2013-07-03T14:52:19.393 回答