如何在 RCP 中右键单击获取行数据

问题描述

我是 RCP 框架的初学者。在此,我使用 Table 和 Table 项目制作了一张 SWT 表。 在我的表中显示一个复选框和几个进度条和其他行数据。我想在右键单击时显示几个菜单项,在右键单击时要打开所选行的视图。我在右键单击获取行数据时遇到困难。我希望右键单击行数据以显示所选行的视图。

public void createPartControl(Composite parent) {
        
        toolkit = new FormToolkit(parent.getdisplay());
        compositeObj = toolkit.createComposite(parent,SWT.NONE);
        GridLayout layout = new GridLayout();
        compositeObj.setLayout(layout);

        GridData gridData = new GridData();
        gridData.verticalAlignment = GridData.FILL;
        gridData.horizontalSpan = 4;
        gridData.grabExcessHorizontalSpace = true;
        gridData.grabExcessverticalSpace = true;
        gridData.horizontalAlignment = GridData.FILL;

        Button button = new Button(compositeObj,SWT.PUSH);
        button.setLayoutData(new GridData(SWT.BEGINNING,SWT.CENTER,false,false));
        button.setText("  Test  ");

        myTable = new Table(compositeObj,SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER | SWT.CHECK);
        myTable.setLayoutData(gridData);

        final int[] bounds = { 30,80,100,50,70,50 };
        for (int i = 0; i < titles.length; i++) {
            TableColumn tblColumn = new TableColumn(myTable,SWT.NONE);
            tblColumn.setText(titles[i]);
            tblColumn.setWidth(bounds[i]);
            tblColumn.setResizable(true);
            tblColumn.setMoveable(true);
            tblColumn.pack();
        }
        myTable.setHeaderVisible(true);
        myTable.setLinesVisible(true);
        
        dataProvider = DetailDao.collectDetails();
        for (int i = 0; i < dataProvider.size(); i++) {
            items[i] = new TableItem(myTable,SWT.NONE);
        }

        ReloadSectionView(parent);
        myTable.addListener(SWT.Selection,new Listener() {
            public void handleEvent(Event e) {
                int i = 0;
                TableItem tableItem[] = myTable.getItems();
                for (i = 0; i < tableItem.length; i++) {
                    if (tableItem[i].getChecked()) {
                        if (tableItem[i].getGrayed()) {
                            tableItem[i].setChecked(false);
                        } 
                        break;
                    }
                }
            }
        });
}
---------------------------------------------------------------------------------
    btsTable.addMouseListener(new MouseListener() {
            @Override
            public void mouseUp(MouseEvent e) {
                if (e.button == mouseButtons_e.RIGHT.ordinal()) {
                        popupMenu(e);
                }
            }
            @Override
            public void mouseDown(MouseEvent e) {
            }
            @Override
            public void mouseDoubleClick(MouseEvent e) {
                btsTable.clear(3);
            }
        });
 --------------------------------------------------------------------------------       
        private static void popupMenu(final MouseEvent e) {
            Menu menu = new Menu(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),SWT.POP_UP);
            MenuItem pView = new MenuItem(menu,SWT.NONE);
            pView.setText("Test View");
            pView.addListener(SWT.Selection,new Listener() {
                public void handleEvent(Event event) {
                    for (int i = 0; i < DefinedConstants.MAX_NO_OF_PROVIDERS; i++) {
                        boolean flag = false;
                        int k = 0;
                        int[] selRows = new int[12];
                        if (e.button == mouseButtons_e.RIGHT.ordinal()) {
                        ....................
                        ....................
                        ???????????????????
                        ...................
                        ...................
                        }
                    }
                }
            });
        }

解决方法

当您创建 Menu 时,您应该只创建一次 Table

Menu menu = new Menu(myTable);
myTable.setMenu(menu);

调用 setMenu 使菜单成为表格的上下文菜单,您不需要使用鼠标侦听器,它将在右键单击时自动使用。

同样,您可以在创建菜单时将 MenuItem 添加到菜单中:

MenuItem item = new MenuItem(menu,SWT.PUSH);
item.setText("Test View");

item.addSelectionListener(SelectionListener.widgetSelectedAdapter((SelectionEvent event) ->
      {
        int index = myTable.getSelectionIndex();

        // TODO handle the selected table item
      }));

根据表项索引(或TableItem)提供获取行数据的方法由您决定

如果您使用的是旧版本的 SWT,请使用 SelectionAdapter

item.addSelectionListener(new SelectionAdapter()
    {
       @Override
       public void widgetSelected(SelectionEvent e)
       {
         int index = myTable.getSelectionIndex();

         // TODO handle the selected table item
       }
    });