JavaFX drop down button

孤街醉人 提交于 2019-12-05 11:15:24

If you do not have to store the selection like in a ChoiceBox you can use for example the MenuButton control.

MenuButton is a button which, when clicked or pressed, will show a ContextMenu.

A MenuButton has the graphicProperty mentioned by you as its base class is Labeled.

Simple example

final Image image = new Image(getClass().getResource("cross_red.png").toExternalForm(), 20, 20, true, true);
MenuButton menuButton = new MenuButton("Don't touch this");
menuButton.setGraphic(new ImageView(image));
menuButton.getItems().addAll(new MenuItem("Really"), new MenuItem("Do not"));

This will produce a button like:

Note: If you need the button also to act like a real button, you can switch from MenuButton to SplitMenuButton. The only difference is that the control field is splitted into a button part and a drop down part (so you can assign an action also for the header):

The SplitMenuButton, like the MenuButton is closely associated with the concept of selecting a MenuItem from a menu. Unlike MenuButton, the SplitMenuButton is broken into two pieces, the "action" area and the "menu open" area.

If the user clicks in the action area, the SplitMenuButton will act similarly to a Button, firing whatever is associated with the ButtonBase.onAction property.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!