Add items to SplitMenuButton and click action
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SplitMenuButton;
import javafx.stage.Stage;
public class Main extends Application {
@Override public void start(Stage stage) {
Group group = new Group();
Scene scene = new Scene(group);
SplitMenuButton m = new SplitMenuButton();
m.setText("Shutdown");
m.getItems().addAll(new MenuItem("Logout"), new MenuItem("Sleep"));
m.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
System.out.println("Shutdown");
}
});
group.getChildren().add(m);
stage.setTitle("Welcome to JavaFX!");
stage.setScene(scene);
stage.sizeToScene();
stage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
Related examples in the same category