Showing a right click menu for a SWT TableItem? - java

Is it possible to show a right click menu on table items with SWT? The menu would be different for every item, e.g for some rows, some of the menu items would be enabled, for others, they would be disabled. So, each row would need its own menu, and when setting up the menu i'd need a way to identify which row I was working with.
Any ideas?

Listening for SWT.MouseDown, as suggested by #user4793956, is completely useless. The context menu is always brought up, no need to call setVisible(true). Quite contrary, you need to cancel the SWT.MenuDetect event, if you do not want the menu to pop up.
This works for me:
// Create context menu
Menu menuTable = new Menu(table);
table.setMenu(menuTable);
// Create menu item
MenuItem miTest = new MenuItem(menuTable, SWT.NONE);
miTest.setText("Test Item");
// Do not show menu, when no item is selected
table.addListener(SWT.MenuDetect, new Listener() {
#Override
public void handleEvent(Event event) {
if (table.getSelectionCount() <= 0) {
event.doit = false;
}
}
});

Without using a DynamicTable:
Menu contextMenu = new Menu(table);
table.setMenu(contextMenu);
MenuItem mItem1 = new MenuItem(contextMenu, SWT.None);
mItem1.setText("Menu Item Test.");
table.addListener(SWT.MouseDown, new Listener(){
#Override
public void handleEvent(Event event) {
TableItem[] selection = table.getSelection();
if(selection.length!=0 && (event.button == 3)){
contextMenu.setVisible(true);
}
}
});

table = new DynamicTable(shell, SWT.BORDER | SWT.FULL_SELECTION);
table.addMenuDetectListener(new MenuDetectListener()
{
#Override
public void menuDetected(MenuDetectEvent e)
{
int index = table.getSelectionIndex();
if (index == -1)
return; //no row selected
TableItem item = table.getItem(index);
item.getData(); //use this to identify which row was clicked.
//The popup can now be displayed as usual using table.toDisplay(e.x, e.y)
}
});
More details: http://www.eclipsezone.com/eclipse/forums/t49734.html

Related

Why does my Menu Listener not respond to first click?

So I have a menu for a table item and I try to add a MenuListener for it. While debugging, on first click on the table, only the listener is created and the event is not handled. On the second click, it behaves as it should.
I have the following code:
private void createWidgets(Composite composite)
{
//some tabs are created
for (int i = 0; i < numberOfTabs; i++)
{
//tabs are populated with tables
this.tables[i].getTableViewer().getTable().addMenuDetectListener(new MenuDetectListener() {
#Override
public void menuDetected(MenuDetectEvent e) {
Table table = (Table) e.getSource();
addMenuForTable(table);
}
});
}
this.tabFolder.setSelection(0);
}
private void addMenuForTable(Table table)
{
final Menu someMenu = new Menu(table);
table.setMenu(someMenu);
someMenu.addMenuListener(new ChangingMenu(this, table, someMenu));
}
And my custom ChangingMenu class looks like this
public class ChangingMenu extends MenuAdapter {
private Menu someMenu;
private SomeView view;
private Table table;
public ChangingMenu(SomeView view, Table viewer, Menu someMenu) {
this.view = view;
this.someMenu= someMenu;
this.table = viewer;
}
#Override
public void menuShown(MenuEvent e) {
MenuItem[] menuItems = this.logChangeMenu.getItems();
for (int i = 0; i < menuItems.length; i++) {
menuItems[i].dispose();
}
addMenu(this.table);
}
private void addMenu(Table table) {
MenuItem menuItem= new MenuItem(this.someMenu, SWT.NONE);
menuItem.setText("Some text here");
//here I have a listener for this menu item that opens some dialog on item click
}
}
So on fist click on table, the Changing Menu is created, but the showMenu is not called somehow. Only on second click I get to see the menu item declared there.
Adding the menu in a menu detect listener is too late, there is no need to use this listener here. Just add the menu as soon as the table has been created:
addMenuForTable(this.tables[i].getTableViewer().getTable());

Deselect a TableItem after clicking in an empty slot in the table

I have a table with many TableItems (Not a tableViewer), when I click on one of the table Items it get selected . The only way to deselect it is by selecting another TableItem. I want to implement a way to deselect The Table selection when The user click on the table Where there is no TableItems, or when ReSelecting the same TableItem.
table.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
if(e.item != ItemSelectioner ) {
ItemSelectioner = (TableItem)e.item;
// Blabla
}else {
ItemSelectioner = null;
table.deselectAll();
//blabla
}
}
});
As you can see, am using a selectionEvent which I think is the probleme, and using:
e.doit = false;
didn't work also.
Selection events are not generated for the empty parts of the table so you can't use a selection listener to do this.
You can use a mouse down listener and check if there is a table item at the mouse location:
table.addListener(SWT.MouseDown, event -> {
TableItem item = table.getItem(new Point(event.x, event.y));
if (item == null) { // No table item at the click location?
table.deselectAll();
}
});
To clear the selection the second time an item is clicked use something like this:
table.addListener(SWT.Selection, new Listener()
{
private int lastSelected = -1;
#Override
public void handleEvent(final Event event)
{
final int selectedIndex = table.getSelectionIndex();
if (selectedIndex < 0) {
lastSelected = -1;
return;
}
if (selectedIndex == lastSelected) {
table.deselect(selectedIndex);
lastSelected = -1;
}
else {
lastSelected = selectedIndex;
}
}
});

Select Row on Rightclick JavaFX TableView

I'm having a table view and I have added a context menu to that. When I right first time it highlight the row and show the context menu. But when I right click again on some other row it show the menu but doesn't highlight the new row and clear the old selected row.
How can I clear the old selected row and highlight the new row ?
I added the context menu using setRowFactorymethod
mytblView.setRowFactory(new Callback<TableView<Rowdata>, TableRow<Rowdata>>() {
#Override
public TableRow<Rowdata> call(TableView<Rowdata> tableView) {
final TableRow<Rowdata> row = new TableRow<>();
final ContextMenu contextMenu = new ContextMenu();
final MenuItem mnuItemAnalyze = new MenuItem("Analyze");
mnuItemAnalyze.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
//logic for menu item
}
});
contextMenu.getItems().add(mnuItemAnalyze);
// Set context menu on row, but use a binding to make it only show for non-empty rows:
row.contextMenuProperty().bind(Bindings.when(row.emptyProperty()).then((ContextMenu) null).otherwise(contextMenu));
return row;
}
});
}

Context Menu on a row of TableView?

I am using JavaFX and my application has a table and I can add elements to the table but I want to create a context menu that displays on a row when I right click on that row.
What I have...
In Scene Builder I have a method that runs on when the Context Menu is activated but that isn't exactly what I want. This would be fine really because I am programmatically grab the selected item from the table whenever I want. The issue, if I keep what I currently have, is getting the context menu to popup at the selected element.
contextMenu is the context menu with menu items.
connectedUsers is the TableView
The following is the closest I can get, but this shows the context menu at the bottom of the TableView
contextMenu.show(connectedUsers, Side.BOTTOM, 0, 0);
I believe that the best solution would be this as discussed in here.
table.setRowFactory(
new Callback<TableView<Person>, TableRow<Person>>() {
#Override
public TableRow<Person> call(TableView<Person> tableView) {
final TableRow<Person> row = new TableRow<>();
final ContextMenu rowMenu = new ContextMenu();
MenuItem editItem = new MenuItem("Edit");
editItem.setOnAction(...);
MenuItem removeItem = new MenuItem("Delete");
removeItem.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
table.getItems().remove(row.getItem());
}
});
rowMenu.getItems().addAll(editItem, removeItem);
// only display context menu for non-empty rows:
row.contextMenuProperty().bind(
Bindings.when(row.emptyProperty())
.then((ContextMenu) null)
.otherwise(rowMenu);
return row;
}
});
JavaFX 8 (with Lambda):
MenuItem mi1 = new MenuItem("Menu item 1");
mi1.setOnAction((ActionEvent event) -> {
System.out.println("Menu item 1");
Object item = table.getSelectionModel().getSelectedItem();
System.out.println("Selected item: " + item);
});
ContextMenu menu = new ContextMenu();
menu.getItems().add(mi1);
table.setContextMenu(menu);
See also:
https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/ContextMenu.html
try this..
ContextMenu cm = new ContextMenu();
MenuItem mi1 = new MenuItem("Menu 1");
cm.getItems().add(mi1);
MenuItem mi2 = new MenuItem("Menu 2");
cm.getItems().add(mi2);
table.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent t) {
if(t.getButton() == MouseButton.SECONDARY) {
cm.show(table, t.getScreenX(), t.getScreenY());
}
}
});

Hiding menu items in TreeViewer

I am trying to hide/unhide a submenu item (menumanager) from the context (popup menu)
when specific node is selected/not selected in the tree.
Although the setVisible methods triggered as expected it doesn't effect.
The Code:
TreeViewer tViewer;
tViewer = new TreeViewer(parent, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
tViewer.setLabelProvider(new WorkbenchLabelProvider());
tViewer.setContentProvider(new BaseWorkbenchContentProvider());
tViewer.setInput(viewFactory.getInstance().getRoot());
final MenuManager menuMain = new MenuManager("Main",null);
menuMain.add(mActionClose);
MenuManager menuManager = new MenuManager("#PopupMenu", "contextMenu");
menuManager.add(menuMain);
menuManager.addMenuListener(new IMenuListener() {
public void menuAboutToShow(IMenuManager manager) {
//Get the selected node in tree
IStructuredSelection selection = (IStructuredSelection) tViewer.getSelection();
if (!selection.isEmpty()) { //If something selected
NNodeBase ob = (NNodeBase) selection.getFirstElement(); //Get the base class of node
if (!(ob instanceof NTMModel)) {
menuMain.setVisible(false);
}
else
menuMain.setVisible(true);
}
}
});
Menu menu = menuManager.createContextMenu(tViewer.getControl());
tViewer.getControl().setMenu(menu);
You must set the manager to recreate the menu before it is shown:
menuManager.setRemoveAllWhenShown(true);
Then, in menuAboutToShow() you add the items, testing the condition you need:
if ((ob instanceof NTMModel)) {
menuManager.add(mActionClose);
} else {
// don't show the menu item
}
You don't need menuMain anymore.

Categories