Zee Best
Zee Best

Reputation: 55

JavaFX Get selected item of a ContextMenu?

Since you can navigate a ContextMenu in JavaFX using the arrow keys, is there a way to retrieve the currently highlighted/selected MenuItem?

I'm basically trying to create an autocomplete feature like outlook etc does with email addresses, where you begin typing and it gives suggestions below, which you can press tab to input the selected menu action all without using the mouse.

Any help is appreciated, thanks.

Upvotes: 1

Views: 2856

Answers (2)

C.Kadura
C.Kadura

Reputation: 51

You can get the selected menu item when you add an onAction listener to the context menu. The target of the ActionEvent is the selected menu item which you can further process.

    contextmenu.setOnAction(e -> System.out.printly(((MenuItem)e.getTarget()).getText()));

Upvotes: 2

methu mwangi
methu mwangi

Reputation: 11

Try using a combo box instead. it gives you a drop down list and an optional text box. Context menu is a menu item and you can only implement a selection model if you wrap it in List view or table view.

Upvotes: 0

Related Questions