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

How to get row data on right click in RCP

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

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, 80, 100, 50, 100, 50, 50, 70, 50, 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()) {
                        ....................
                        ....................
                        ???????????????????
                        ...................
                        ...................
                        }
                    }
                }
            });
        }

您应该在创建 Table 时只创建一次 Menu

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

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

同样,您可以在创建菜单时将 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
      }));

给定 table 项目索引(或者 TableItem

,由您提供一种获取行数据的方法

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

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

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