This article is a tutorial on the JavaFX MenuButton.
The MenuButton is a widget that looks just like a regular button, but when clicked it reveals a drop down list of options from which the user may select one. The MenuButton is a good choice for GUI’s with a limited number of space to be displaying many options.
The JavaFX MenuButton control is represented by the class javafx.scene.control.MenuButton
. A list of helpful and important methods for this widget can be found at the bottom of this tutorial.
JavaFX MenuButton Example
To create a MenuButton we need the help of another class called MenuItem. Using this class we will individually create the Options we wish to present and then add them into the MenuButton object that we created.
Passing the three Item objects into the MenuButton class automatically inserts them into the widget.
package application;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.MenuButton;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.VBox;
public class Tutorial extends Application {
public static void main(String args[]){
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
VBox layout = new VBox();
Scene scene = new Scene(layout, 300, 300);
MenuItem Item1 = new MenuItem("Option 1");
MenuItem Item2 = new MenuItem("Option 2");
MenuItem Item3 = new MenuItem("Option 3");
MenuButton menuButton = new MenuButton("Options", null, Item1, Item2, Item3);
layout.getChildren().add(menuButton);
primaryStage.setTitle("CodersLegacy");
primaryStage.setScene(scene);
primaryStage.show();
}
}
The output of the above code:
Another alternate way of adding the Items into the menubutton is through using the add function. The following three lines represent the new addition in the code that would be required.
menuButton.getItems().add(Item1);
menuButton.getItems().add(Item2);
menuButton.getItems().add(Item3);
Retrieving MenuButton Selection
If you want to retrieve the option that the user selected from the MenuButton, you will have to use the getText()
method and event handling.
We will have to individually map each MenuItem’s event handling code to the a lambda function. What you keep inside the function is upto you of course, but for now we have a simple line that is displayed when an option is selected.
package application;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.MenuButton;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.VBox;
public class Tutorial extends Application {
public static void main(String args[]){
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
VBox layout = new VBox();
Scene scene = new Scene(layout, 300, 300);
MenuItem Item1 = new MenuItem("Option 1");
MenuItem Item2 = new MenuItem("Option 2");
MenuItem Item3 = new MenuItem("Option 3");
MenuButton menuButton = new MenuButton("Options", null, Item1, Item2, Item3);
layout.getChildren().add(menuButton);
Item1.setOnAction(event -> {
System.out.println("Option 1 selected");
});
Item2.setOnAction(event -> {
System.out.println("Option 2 selected");
});
Item3.setOnAction(event -> {
System.out.println("Option 3 selected");
});
primaryStage.setTitle("CodersLegacy");
primaryStage.setScene(scene);
primaryStage.show();
}
}
Every time you select one of these options, the corresponding output will display on the console screen.
JavaFX MenuButton Methods
A list of useful and important methods which can be used on the MenuButton widget.
Methods | Description |
---|---|
getItems() | Returns the list of items assigned to the widget. |
isShowing() | Returns a True of False value depending on whether the Menu is showing. |
setPopupSide(side) | Determines where the Menu shows up from. |
getPopupSide() | Returns the value of the Popupside. |
show() | Shows the context menu. |
This marks the end of the JavaFX MenuButton tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the article content can be asked in the comments section below.
Head back to the main JavaFX Tutorial to learn about the other great GUI components!