JavaFX drop-down button

How to create a "drop-down button" in JavaFX?

So far, I'm using it Choice@R_206_2419 @, now I have to Choice@R_206_2419 @Assign an image,

So I'm going to change it into a drop-down button So I can set an icon

I use scenebuilder to design UI

There is no help searching how to create drop - down buttons using JavaFX

Solution

If you don't have to be like Choice@R_206_2419 @To store selections as in, you can use, for example, a MenuButton control

A MenuButton has the graphicproperty you mentioned, because its base class is labeled

Simple example

final Image image = new Image(getClass().getResource("cross_red.png").toExternalForm(),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 generate a button, such as:

Note: if you want the button to be like a real button, you can switch from MenuButton to split MenuButton The only difference is that the control field is divided into button part and drop-down part (so you can also assign actions as titles):

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>