Context Menu on a row of TableView? - java

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

Related

JavaFX TreeView ContextMenu for each TreeItem [duplicate]

I want to have a context menu on a TreeView item.
I am expecting that the ActionEvent of the event handler gives me information about which TreeView item is clicked, but I just find that getSource and getTarget return a Menu Item. How can I find out which TreeView Item has been clicked? I can have multiple treevIews in separate Tabs.
FileContextMenu cm = new FileContextMenu(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
System.out.println("Open File");
//MenuItem mi = (MenuItem)e.getSource();
EventTarget et = e.getTarget();
//File editorFile = new File(mi.getId());
System.out.println(et);
//mainWindowController.openEditor(editorFile);
}
}, new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
System.out.println("Create Project From Template");
}
});
which calls this:
public class FileContextMenu extends ContextMenu
{
public FileContextMenu(EventHandler<ActionEvent> ehOpenFile,
EventHandler<ActionEvent> ehProjectFromTemplate)
{
MenuItem item1 = new MenuItem("Open File");
item1.setOnAction(ehOpenFile);
MenuItem item2 = new MenuItem("Create Project From Template");
item2.setOnAction(ehProjectFromTemplate);
this.getItems().addAll(item1, item2);
}
}
I am attaching the menu here:
private void addFilesTab(FXMLLoader loader, String sRoot, ContextMenu cm) throws IOException
{
AnchorPane fileView = loader.load();
FileViewController fileViewController = loader.getController();
FileShort fsRoot = new FileShort(sRoot);
if(fsRoot.exists()) {
fileViewController.setRootFolder(fsRoot);
fileViewController.setContextMenu(cm);
ObservableList<Tab> tabs = navigationTabPane.getTabs();
tabs.add(new Tab(sRoot));
// Connect the FileView with last tab of the Navigation TabPane.
tabs.get(tabs.size()-1).setContent(fileView);
}
}
which calls this:
public void setContextMenu(ContextMenu cm)
{
fileTreeView.setContextMenu(cm);
}
I now try to use a cellfactory, but I don't understand how to use the p parameter to find a cells value . My code for this is:
this.fileTreeView.setCellFactory(new Callback<TreeView<FileShort>,TreeCell<FileShort>>(){
#Override
public TreeCell<FileShort> call(TreeView<FileShort> p) {
TreeCell<FileShort> cell = new TreeCell<FileShort>();
cell.setContextMenu(cm);
return cell;
}
});
You have to create a different context menu for each cell:
this.fileTreeView.setCellFactory(new Callback<TreeView<FileShort>,TreeCell<FileShort>>(){
#Override
public TreeCell<FileShort> call(TreeView<FileShort> p) {
TreeCell<FileShort> cell = new TreeCell<FileShort>() {
#Override
protected void updateItem(FileShort file, boolean empty) {
super.updateItem(file, empty);
if (empty) {
setText(null);
} else {
// maybe use a more appropriate string for display here
// e.g. if you were using a regular java.io.File you would
// likely want file.getName()
setText(file.toString());
}
}
};
ContextMenu cm = createContextMenu(cell);
cell.setContextMenu(cm);
return cell;
}
});
private ContextMenu createContextMenu(TreeCell<FileShort> cell) {
ContextMenu cm = new ContextMenu();
MenuItem openItem = new MenuItem("Open File");
openItem.setOnAction(event -> {
FileShort file = cell.getItem();
if (file != null) {
// open the file...
}
});
cm.getItems().add(openItem);
// other menu items...
return cm ;
}

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

JavaFX CheckBoxTreeItem Selection

I have a javafx checkbox tree. I need to select the checkbox when the tree item is clicked. I have added a listener for the selection property of the tree view. But the listener gets fired only when the tree item is clicked. The above listener is not fired when the checkbox is clicked.
Required: A listener that fires when a tree item or checkbox is clicked in the treeview.
Code:
String memberArray = {"subChild1", "subChild2", "childSub1"}
Group groupRoot = new Group();
Scene scene = new Scene(groupRoot, Color.ALICEBLUE);
HBox hBox = new HBox();
hBox.setMaxWidth(fxPanel.getWidth());
final Label royalLabel = new Label("Select a item");
TreeSet<String> prefixMember = new TreeSet<String>();
String tmpName = null;
LinkedHashSet<CheckBoxTreeItem<String>> treeItems = new LinkedHashSet<CheckBoxTreeItem<String>>();
LinkedHashSet<CheckBoxTreeItem<String>> treeSubItems = new LinkedHashSet<CheckBoxTreeItem<String>>();
for (String item : memberArray) {
if (!item.isEmpty()) {
tmpName = item.substring(0, 3);
prefixMember.add(tmpName);
}
}
// Create and empty TreeView
TreeView<String> duckTree = new TreeView<String>();
// Create TreeItems for the Hierarchy of the TreeView
CheckBoxTreeItem<String> root = new CheckBoxTreeItem<String>("Parent");
CheckBoxTreeItem<String> lm1 = new CheckBoxTreeItem<String>("Child1");
CheckBoxTreeItem<String> lm2 = new CheckBoxTreeItem<String>("Child2");
for (String item : prefixMember) {
CheckBoxTreeItem<String> treeItem = new CheckBoxTreeItem<String>(item.toString());
for (String subItem : memberArray) {
if (!subItem.isEmpty() && subItem.substring(0, 3).equals(item)) {
CheckBoxTreeItem<String> treeSubItem = new CheckBoxTreeItem<String>(
subItem.toString());
treeSubItems.add(treeSubItem);
}
}
treeItems.add(treeItem);
treeItem.getChildren().addAll(treeSubItems);
treeSubItems.clear();
}
root.getChildren().addAll(treeItems);
treeItems.clear();
// Create a TreeView using the root TreeItem
TreeView<String> royalTree = new TreeView<String>(root);
royalTree.setCellFactory(CheckBoxTreeCell.<String>forTreeView());
// Set a ChangeListener to handle events that occur with a Treeitem
// is selected
royalTree.getSelectionModel().selectedItemProperty()
.addListener(new ChangeListener<TreeItem<String>>() {
public void changed(
ObservableValue<? extends TreeItem<String>> observableValue,
TreeItem<String> oldItem, TreeItem<String> newItem) {
// Gets fired only on selection of tree item
// Need to get fired on selection of check box too
// Select the respective checkbox on selection of tree item
}
});
hBox.getChildren().add(royalTree);
groupRoot.getChildren().add(hBox);
fxPanel.setScene(scene);
You could just add an EventHandler to your root item in the tree:
rootItem.addEventHandler(CheckBoxTreeItem.checkBoxSelectionChangedEvent(), new EventHandler<TreeModificationEvent<Object>>() {
#Override
public void handle(TreeModificationEvent<Object> event) {
// Your code here.
}
});
i had the same problem and searched looong time. Sadly there is not offical documentation for this from oracle.
The answer is to set the CellFactory and call the getSelectedStateCallback().call(this.getTreeItem());
for your treeItem in the updateItem:
// set cellFactory
royalTree.setCellFactory(new Callback<TreeView<String>, TreeCell<String>>() {
#Override
public TreeCell<String> call(TreeView<String> p) {
// return new CheckBoxTreeCell, you also can make a new class with this
return new CheckBoxTreeCell<String>() {
#Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (!empty) {
// call the selectedStat Callback for treeitem
ObservableValue<Boolean> selectedState = getSelectedStateCallback().call(this.getTreeItem());
if (selectedState != null) {
// do something here
}
}
}
};
}
});
i have tested this in fx 8, but it should also work in fx 2.2
happy coding,
kalasch
Yes, adding an event handler to the tree item works. Here is some example code (Java 8) with a TreeView with items as CheckBoxTreeItem:
CheckBoxTreeItem<Path> rootItem = new CheckBoxTreeItem<>(rootDirPath);
rootItem.addEventHandler(
CheckBoxTreeItem.<Path>checkBoxSelectionChangedEvent(),
(TreeModificationEvent<Path> e) -> {
CheckBoxTreeItem<Path> item = e.getTreeItem();
if (item.isSelected() || item.isIndeterminate()) {
System.out.println("Some items are checked");
}
else {
System.out.println("Some items are unchecked");
}
});
TreeView<Path> tree = new TreeView<>(rootItem);
tree.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
Do you require an event for each selection immediately? If not you can create an arraylist of all your checkboxtreeitems and iterate through that to check for selected or not selected when you need it.

Showing a right click menu for a SWT TableItem?

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

JavaFX ListView & ContextMenu - getSelectedItem() returns null

I want to use a context menu item on the lines of a listView. In the event handler of the listView's MOUSE_CLICKED event, the getSelectionModel().getSelectedItem() returns the selected item, thats ok. But, when I handle the contextMenuItem's onAction event, it returns null. However, graphically the item is selected.
Is there a way to "keep" the selection after the first event handling?
Here is the relevant part of the code:
ListView<Text> nameList = new ListView<>();
final ContextMenu cCm = new ContextMenu();
MenuItem cItem = new MenuItem("someText");
cCm.getItems().add(cItem);
...
nameList.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent e) {
if (e.getButton() == MouseButton.SECONDARY) {
//its OK here:
System.out.println(nameList.getSelectionModel().getSelectedItem().getText());
cCm.show(nameList, e.getScreenX(), e.getScreenY());
}
}
});
cItem.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent e) {
final Stage dialog = new Stage();
dialog.initModality(Modality.WINDOW_MODAL);
//nullPointerException on the following:
Text t = new Text(nameList.getSelectionModel().getSelectedItem().getText());
//showing dialog, etc.
I pretty much created an exact replica of what you did, and my implementation worked:
private void initRandomCardListView() {
populateRandomList();
final ContextMenu randomListContextMenu = new ContextMenu();
MenuItem replaceCardMenuItem = new MenuItem("Replace");
replaceCardMenuItem.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
replaceRandomCard();
}
});
randomListContextMenu.getItems().add(replaceCardMenuItem);
randomCardList.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
if (event.getButton().equals(MouseButton.SECONDARY)) {
randomListContextMenu.show(randomCardList, event.getScreenX(), event.getScreenY());
}
}
});
}
private void replaceRandomCard() {
System.out.println("jobs done");
System.out.println("card selected: " + randomCardList.selectionModelProperty().get().getSelectedItem().toString());
System.out.println("card index: " + randomCardList.getSelectionModel().getSelectedIndex());
System.out.println("card index: " + randomCardList.getSelectionModel().getSelectedItem().toString());
}
I don't have any null pointer exceptions. Overall your implementation looks good. There is most likely something that is wrong with the items in your listview.

Categories