0

I am beginner of RCP framework. In this I have made one SWT table by using Table and Table item. In my table one check box and couple of progress bar and other row data is showing. I want to show couple of menu items on right click and on right click want to open view for selected row. I am facing difficulty in getting row data on right click. I want right clicked row data to show view of selected row.

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()) {
                        ....................
                        ....................
                        ???????????????????
                        ...................
                        ...................
                        }
                    }
                }
            });
        }
Dum
  • 13
  • 3
  • Show us the code you have so far. – greg-449 Apr 13 '21 at 12:46
  • Also note that it is often easier to use the JFace TableViewer with a content and label provider instead of SWT Table/TableItem, this is especially the case in Eclipse RCPs which provide more support for TableViewer. – greg-449 Apr 13 '21 at 14:19
  • Please find the code written so far..... – Dum Apr 14 '21 at 04:19

1 Answers1

0

You should only create the Menu once when you create the Table.

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

Calling setMenu makes the menu the context menu for the table, you do not need to use a mouse listener, it will be used automatically on right click.

Again you can add the MenuItem to the menu once, when you create the menu:

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
      }));

It is up to you to provide a way to get to the row data given a table item index (or alternatively the TableItem)

If you are using old versions of SWT use SelectionAdapter:

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

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

greg-449
  • 109,219
  • 232
  • 102
  • 145
  • Hi greg-449 thanks for your response ....still i am facing issue because i am using Version: Neon Release (4.6.0) in this version widgetSelectedAdapter method not available. – Dum Apr 15 '21 at 14:40
  • Use `SelectionAdapter` for old versions of Eclipse - see updated answer, but note that Neon is now 5 years and 13 releases old – greg-449 Apr 15 '21 at 15:09
  • Thanks greg-449, – Dum Apr 15 '21 at 17:49
  • Hello greg-449 in our table i am using Tableeditor for some columns (for Progress bar, for setting Labels) and i want to clear the table i tried Table.removeAll(); but it clear texual content only. i also tried editor[i].dispose(); and mylable[i].setText(""); editor[i].setEditor(Label[i], Table.getItem(i), 11); but lable and progress bar are still showing .can you please help me one more time. – Dum Apr 16 '21 at 04:08
  • Please don't ask new questions in comments - ask a new question and show your code. – greg-449 Apr 16 '21 at 06:35
  • "Unable to clear the table in RCP" Added greg-449 "https://stackoverflow.com/questions/67262306/unable-to-clear-the-table-in-rcp" – Dum Apr 26 '21 at 07:15