To Handle radio menu item change event via a change listener in JavaFX
// Use a change listener to respond to changes in the radio // menu item setting. tg.selectedToggleProperty().addListener(new ChangeListener<Toggle>() { public void changed(ObservableValue<? extends Toggle> changed, Toggle oldVal, Toggle newVal) { if (newVal == null) return;//from w w w .j a v a2 s. c o m // Cast newVal to RadioButton. RadioMenuItem rmi = (RadioMenuItem) newVal; // Display the selection. response.setText("Priority selected is " + rmi.getText()); } });
Full source
import javafx.application.Application; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.control.Menu; import javafx.scene.control.MenuBar; import javafx.scene.control.MenuItem; import javafx.scene.control.RadioMenuItem; import javafx.scene.control.SeparatorMenuItem; import javafx.scene.control.Toggle; import javafx.scene.control.ToggleGroup; import javafx.scene.layout.BorderPane; import javafx.stage.Stage; public class Main extends Application { //Create a label that will report the selection. private Label response = new Label("Menu Demo"); public static void main(String[] args) { launch(args);// www. j a v a 2s . co m } public void start(Stage myStage) { myStage.setTitle("Menus from java2s.com"); // Use a BorderPane for the root node. BorderPane rootNode = new BorderPane(); Scene myScene = new Scene(rootNode, 300, 300); myStage.setScene(myScene); // Create the menu bar. MenuBar mb = new MenuBar(); // Create the File menu, including a mnemonic. Menu fileMenu = new Menu("_File"); MenuItem open = new MenuItem("Open"); MenuItem close = new MenuItem("Close"); MenuItem save = new MenuItem("Save"); MenuItem exit = new MenuItem("Exit"); // Create the Priority sub menu. Menu priorityMenu = new Menu("Priority"); // Use radio menu items for the priority setting. RadioMenuItem high = new RadioMenuItem("High"); RadioMenuItem low = new RadioMenuItem("Low"); priorityMenu.getItems().addAll(high, low); // Create a toggle group and use it for the radio menu items. ToggleGroup tg = new ToggleGroup(); high.setToggleGroup(tg); low.setToggleGroup(tg); // Use a change listener to respond to changes in the radio // menu item setting. tg.selectedToggleProperty().addListener(new ChangeListener<Toggle>() { public void changed(ObservableValue<? extends Toggle> changed, Toggle oldVal, Toggle newVal) { if (newVal == null) return; // Cast newVal to RadioButton. RadioMenuItem rmi = (RadioMenuItem) newVal; // Display the selection. response.setText("Priority selected is " + rmi.getText()); } }); // Select High priority for the default selection. high.setSelected(true); fileMenu.getItems().addAll(priorityMenu,open, close, save, new SeparatorMenuItem(), exit); // Add File menu to the menu bar. mb.getMenus().add(fileMenu); // the response label to the center position. rootNode.setTop(mb); rootNode.setCenter(response); myStage.show(); } }