Javafx 2.0 - Get ContextMenu Parent Node in EventHandler

I am writing a javafx user interface and would like to get the Node owner of the context menu from eventHandler for the MenuItem object that was clicked.

My code is:

TabPane tabPane = new TabPane(); Tab tab1 = new Tab(); Tab tab2 = new Tab(); tabPane.getTabs().addAll(tab1,tab2); ContextMenu contextMenu = new ContextMenu(); MenuItem menuItem = new MenuItem("Do Some Action"); menuItem.setOnAction(new EventHandler<ActionEvent>(){ @override public void handle(ActionEvent e){ // Get the tab which was clicked on and do stuffs with it } }); contextMenu.getItems().add(menuItem); for(Tab tab: tabPane.getTabs()){ tab.setContextMenu(contextMenu); } 

What I would like to do is get a link to the tab in which it was selected by contextMenu.

I managed to get a link to what ContextMenu seems to be in MenuItem with the following code inside the handle method (ActionEvent e) for the menuItem eventHandler element:

 ContextMenu menu = ((ContextMenu)((MenuItem)e.getSource()).getParentPopup()); 

My idea was to use the ContextMenu.getOwnerNode () method on the menu, and then there is a link to the tab, but when I start, I get a link to an element that I cannot understand.

The toString () method for the object returned by .getOwnerNode () returns "TabPaneSkin $ TabHeaderSkin $ 3 @ 14f59cef", which I cannot understand the value.

Is my approach to working in a chain appropriate until I get back to the Node rule or is there a completely different approach that works better?

All I need is the functionality of ContextMenu, and when MenuItem is clicked, I need to have a link to the tab for which ContextMenu was selected, so I can do cool things with it.

+4
source share
1 answer
  • Create a ContextMenu for each tab.
  • Make each final tab.
  • Directly link to the last tab in the menu item's event handler for the context menu.

Here is the code snippet:

 final Tab tab = new Tab("xyzzy"); ContextMenu contextMenu = new ContextMenu(); MenuItem menuItem = new MenuItem("Do Some Action"); menuItem.setOnAction(new EventHandler<ActionEvent>(){ @Override public void handle(ActionEvent e){ tab.setText("Activated by User"); } }); 

Each time the user right-clicks on the title of a tab and selects the "Count Click" menu, the contents of the corresponding tab are updated to count the number of faces counted so far for this tab.

clickcounter

Here is an example executable file:

 import javafx.application.*; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.stage.Stage; public class TabContext extends Application { @Override public void start(Stage stage) { TabPane tabPane = new TabPane(); tabPane.getTabs().addAll( createTab("xyzzy", "aliceblue"), createTab("frobozz", "blanchedalmond") ); stage.setScene(new Scene(tabPane)); stage.show(); } private Tab createTab(String tabName, String webColor) { final Label content = new Label("0"); content.setAlignment(Pos.CENTER); content.setPrefSize(200, 100); content.setStyle("-fx-font-size: 30px; -fx-background-color: " + webColor + ";"); final Tab tab = new Tab(tabName); tab.setContent(content); ContextMenu contextMenu = new ContextMenu(); MenuItem menuItem = new MenuItem("Count Click"); menuItem.setOnAction(new EventHandler<ActionEvent>(){ @Override public void handle(ActionEvent e){ content.setText( "" + (Integer.parseInt(content.getText()) + 1) ); } }); contextMenu.getItems().add(menuItem); tab.setContextMenu(contextMenu); return tab; } public static void main(String[] args) { Application.launch(args); } } 

Alternatively, using this anonymous inner class, you can subclass an EventHandler with a constructor that includes the Tab for which the EventHandler is attached.

 class TabContextMenuHandler implements EventHandler<ActionEvent> { private final Tab tab; TabContextMenuHandler(Tab tab) { this.tab = tab; } @Override public void handle(ActionEvent event) { tab.setText("Activated by User"); } } 
+4
source

Source: https://habr.com/ru/post/1483213/


All Articles