JavaFX SplitMenuButton
Jakob Jenkov |
The JavaFX SplitMenuButton control can show a list of menu options which the user can choose from,
as well as a button which the user can click on when a menu option has been chosen.
The JavaFX SplitMenuButton can show or hide the menu items. The menu items are usually shown when a
little arrow button is clicked in the SplitMenuButton. The JavaFX SplitMenuButton control is represented
by the class javafx.scene.control.SplitMenuButton
. Here is a screenshot of a
JavaFX SplitMenuButton
:
Create SplitMenuButton
Before you can use the JavaFX SplitMenuButton
you must create an instance of it.
Here is an example of creating a JavaFX SplitMenuButton
:
SplitMenuButton splitMenuButton = new SplitMenuButton();
SplitMenuButton Text
You can set the SplitMenuButton
's button text via its setText()
method. Here is
an example of setting the button text of a JavaFX SplitMenuButton
:
splitMenuButton.setText("Click here!");
SplitMenuButton Font
The JavaFX SplitMenuButton enables you to set the font used to render the text of the SplitMenuButton. You can read more about creating fonts in JavaFX in my JavaFX Fonts tutorial. Here is an example of setting a font on a JavaFX SplitMenuButton:
SplitMenuButton splitMenuButton = new SplitMenuButton(); Font font = Font.font("Courier New", FontWeight.BOLD, 36); splitMenuButton.setFont(font);
Set SplitMenuButton Menu Items
You can set the menu items to display in the menu part of a JavaFX SplitMenuButton
via its
MenuItem
collection returned by getItems()
. Each menu item is represented by
a MenuItem
object. Here is an example of setting three menu items on a JavaFX SplitMenuButton
:
MenuItem choice1 = new MenuItem("Choice 1"); MenuItem choice2 = new MenuItem("Choice 2"); MenuItem choice3 = new MenuItem("Choice 3"); button.getItems().addAll(choice1, choice2, choice3);
Respond to Menu Item Selection
The JavaFX SplitMenuButton
works similarly to the JavaFX MenuButton
when it comes to responding to selected menu items. To respond to selection of a menu item in a
JavaFX SplitMenuButton you must set an action listener on each MenuItem
added to the
SplitMenuButton
. Here is an example of responding to menu item selection in a
JavaFX SplitMenuButton
by setting action listeners on its MenuItem
instances:
MenuItem choice1 = new MenuItem("Choice 1"); MenuItem choice2 = new MenuItem("Choice 2"); MenuItem choice3 = new MenuItem("Choice 3"); choice1.setOnAction((e)-> { System.out.println("Choice 1 selected"); }); choice2.setOnAction((e)-> { System.out.println("Choice 2 selected"); }); choice3.setOnAction((e)-> { System.out.println("Choice 3 selected"); });
In this example the action listeners simply print a text to the console. In a real application you would probably want to store information about what action was selected, or take some other action, rather than just printing a text out to the console.
Respond to Button Click
You can respond to JavaFX SplitMenuButton
button clicks by setting an action listener on it.
Here is an example of setting an action listener on a JavaFX SplitMenuButton
:
splitMenuButton.setOnAction((e) -> { System.out.println("SplitMenuButton clicked!"); });
This example uses a Java Lambda Expression
as action listener. When the button is clicked, the text SplitMenuButton clicked!
will be printed
to the console.
SplitMenuButton vs. MenuButton, ChoiceBox and ComboBox
You might be wondering what the difference is between a JavaFX SplitMenuButton
and a
JavaFX MenuButton, JavaFX ChoiceBox and a
JavaFX ComboBox. I will try to explain that below.
The SplitMenuButton
and MenuButton
controls are buttons. That means, that
they are intended for your application to respond to clicks on either one of the menu items, or in the case
of the SplitMenuButton
- the primary button or one of the menu items. Use one of these two controls
when you want an immediate action to follow when the user clicks / selects a menu item. Use the
SplitMenuButton
when one of the choices is done more often than the rest. Use the button part
for the most selected choice, and the menu items for the less often selected choices.
The ChoiceBox
and ComboBox
merely store internally what choices the user has made
among their menu items. They are not designed for immediate action upon menu item selection. Use these controls
in forms where the user has to make several choices before finally clicking either an "OK" or "Cancel" button.
When on of these buttons are clicked, you can read what menu item is chosen from the
ChoiceBox
or ComboBox
.
Tweet | |
Jakob Jenkov |