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()) {
....................
....................
???????????????????
...................
...................
}
}
}
});
}
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
}
});