In this tutorial we will learn more JavaFX widgets usage via simple examples.

Example 1: Menubutton

Let us look at the code

Step 1: Create Project

  1. Open your favorite Java IDE.
  2. In the menu go to File --> Create New Project.

Step 2: Dependencies

No dependencies are needed for this project.

Step 3: Write Code

Our code will comprise the following java files:

  • MenuButtonExample.java
  • MenuButtonFontExample.java
  • MenuButtonIconExample.java
  1. In your editor or IDE, create a file known as MenuButtonExample.java.
  2. Then add the following code:

(a). MenuButtonExample.java

Like in every program we write, we will need to import functionalities into our project. Add the following imports:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.MenuButton;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

Extend the class as shown below:

public class MenuButtonExample extends Application {

Our class will have the following methods:

  • void main(String[] args)
  • void start(Stage primaryStage)

Let’s create a main method as shown below:

    public static void main(String[] args) {

In this particular class we will be overriding our void start(Stage primaryStage) method.
Prepend the code>@Override</code modifier to your method. Then add implementation code as follows:

    @Override
    public void start(Stage primaryStage) {

        //First, simple example of creating a MenuButton
        MenuItem menuItem1 = new MenuItem("Action 1");
        MenuItem menuItem2 = new MenuItem("Action 2");
        MenuItem menuItem3 = new MenuItem("Action 3");

        menuItem1.setOnAction((event) -> { System.out.println("MenuItem1 activated"); });
        menuItem2.setOnAction((event) -> { System.out.println("MenuItem2 activated"); });
        menuItem3.setOnAction((event) -> { System.out.println("MenuItem3 activated"); });

        MenuButton menuButton = new MenuButton("Actions", null, menuItem1, menuItem2, menuItem3);

        //second, more elaborate example of how a MenuButton can be created, and configured via setter methods
        MenuButton menuButton2 = new MenuButton();
        menuButton2.setText("More Actions");

        MenuItem menuItem2_1 = new MenuItem("Action 1");
        MenuItem menuItem2_2 = new MenuItem("Action 2");
        MenuItem menuItem2_3 = new MenuItem("Action 3");

        menuItem2_1.setOnAction((event) -> { System.out.println("MenuItem2_1 activated"); });
        menuItem2_2.setOnAction((event) -> { System.out.println("MenuItem2_2 activated"); });
        menuItem2_3.setOnAction((event) -> { System.out.println("MenuItem2_3 activated"); });

        menuButton2.getItems().add(menuItem2_1);
        menuButton2.getItems().add(menuItem2_2);
        menuButton2.getItems().add(menuItem2_3);

        VBox vbox = new VBox(menuButton, menuButton2);
        Scene scene = new Scene(vbox);
        primaryStage.setScene(scene);
        primaryStage.setWidth (300);
        primaryStage.setHeight(300);
        primaryStage.show();
    }

Here is the full code:

package com.jenkov.javafx.menubutton;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.MenuButton;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * Shows a simple JavaFX MenuButton - and prints a text when a menu item from the button is selected.
 */
public class MenuButtonExample extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        //First, simple example of creating a MenuButton
        MenuItem menuItem1 = new MenuItem("Action 1");
        MenuItem menuItem2 = new MenuItem("Action 2");
        MenuItem menuItem3 = new MenuItem("Action 3");

        menuItem1.setOnAction((event) -> { System.out.println("MenuItem1 activated"); });
        menuItem2.setOnAction((event) -> { System.out.println("MenuItem2 activated"); });
        menuItem3.setOnAction((event) -> { System.out.println("MenuItem3 activated"); });

        MenuButton menuButton = new MenuButton("Actions", null, menuItem1, menuItem2, menuItem3);

        //second, more elaborate example of how a MenuButton can be created, and configured via setter methods
        MenuButton menuButton2 = new MenuButton();
        menuButton2.setText("More Actions");

        MenuItem menuItem2_1 = new MenuItem("Action 1");
        MenuItem menuItem2_2 = new MenuItem("Action 2");
        MenuItem menuItem2_3 = new MenuItem("Action 3");

        menuItem2_1.setOnAction((event) -> { System.out.println("MenuItem2_1 activated"); });
        menuItem2_2.setOnAction((event) -> { System.out.println("MenuItem2_2 activated"); });
        menuItem2_3.setOnAction((event) -> { System.out.println("MenuItem2_3 activated"); });

        menuButton2.getItems().add(menuItem2_1);
        menuButton2.getItems().add(menuItem2_2);
        menuButton2.getItems().add(menuItem2_3);

        VBox vbox = new VBox(menuButton, menuButton2);
        Scene scene = new Scene(vbox);
        primaryStage.setScene(scene);
        primaryStage.setWidth (300);
        primaryStage.setHeight(300);
        primaryStage.show();
    }

}
  1. Next create another file known as MenuButtonFontExample.java.
  2. And add the following code:

(b). MenuButtonFontExample.java

Like in every program we write, we will need to import functionalities into our project. Add the following imports:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.MenuButton;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;

Extend the class as shown below:

public class MenuButtonFontExample extends Application {

Our class will have the following methods:

  • void main(String[] args)
  • void start(Stage primaryStage)

Let’s create a main method as shown below:

    public static void main(String[] args) {

In this particular class we will be overriding our void start(Stage primaryStage) method.
Prepend the code>@Override</code modifier to your method. Then add implementation code as follows:

    @Override
    public void start(Stage primaryStage) {

        MenuItem menuItem1 = new MenuItem("Option 1");
        MenuItem menuItem2 = new MenuItem("Option 2");
        MenuItem menuItem3 = new MenuItem("Option 3");

        MenuButton menuButton = new MenuButton("Options", null, menuItem1, menuItem2, menuItem3);

        Font font = Font.font("Courier New", FontWeight.BOLD, 36);
        menuButton.setFont(font);

        VBox vbox = new VBox(menuButton);
        Scene scene = new Scene(vbox);
        primaryStage.setScene(scene);
        primaryStage.setWidth (300);
        primaryStage.setHeight(300);
        primaryStage.show();
    }

Here is the full code:

package com.jenkov.javafx.menubutton;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.MenuButton;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;

/**
 * Shows a simple JavaFX Button - and prints a text when the button is clicked.
 */
public class MenuButtonFontExample extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        MenuItem menuItem1 = new MenuItem("Option 1");
        MenuItem menuItem2 = new MenuItem("Option 2");
        MenuItem menuItem3 = new MenuItem("Option 3");

        MenuButton menuButton = new MenuButton("Options", null, menuItem1, menuItem2, menuItem3);

        Font font = Font.font("Courier New", FontWeight.BOLD, 36);
        menuButton.setFont(font);

        VBox vbox = new VBox(menuButton);
        Scene scene = new Scene(vbox);
        primaryStage.setScene(scene);
        primaryStage.setWidth (300);
        primaryStage.setHeight(300);
        primaryStage.show();
    }

}
  1. Next create another file known as MenuButtonIconExample.java.
  2. And add the following code:

(c). MenuButtonIconExample.java

Like in every program we write, we will need to import functionalities into our project. Add the following imports:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.MenuButton;
import javafx.scene.control.MenuItem;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

Like in every program we write, we will need to import functionalities into our project. Add the following imports:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

Extend the class as shown below:

public class MenuButtonIconExample extends Application {

Our class will have the following methods:

  • void main(String[] args)
  • void start(Stage primaryStage) throws IOException

Let’s create a main method as shown below:

    public static void main(String[] args) {

In this particular class we will be overriding our void start(Stage primaryStage) throws IOException method.
Prepend the code>@Override</code modifier to your method. Then add implementation code as follows:

    @Override
    public void start(Stage primaryStage) throws IOException {

        //load MenuButton icon
        ImageView imageView = loadIcon();

        MenuItem menuItem1 = new MenuItem("User 1");
        MenuItem menuItem2 = new MenuItem("User 2");

        //create MenuButton - pass ImageView via constructor
        MenuButton menuButton1 = new MenuButton("Show User", imageView, menuItem1, menuItem2);

        MenuItem menuItem2_1 = new MenuItem("User 1");
        MenuItem menuItem2_2 = new MenuItem("User 2");

        //second example - pass ImageView via setGraphic()
        ImageView imageView2 = loadIcon();
        MenuButton menuButton2 = new MenuButton("Show User", null, menuItem2_1, menuItem2_2);
        menuButton2.setGraphic(imageView2);

        VBox vbox = new VBox(menuButton1, menuButton2);
        Scene scene = new Scene(vbox);
        primaryStage.setScene(scene);
        primaryStage.setWidth (300);
        primaryStage.setHeight(300);
        primaryStage.show();
    }

Here is the full code:

package com.jenkov.javafx.menubutton;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.MenuButton;
import javafx.scene.control.MenuItem;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * Shows a simple JavaFX Button - and prints a text when the button is clicked.
 */
public class MenuButtonIconExample extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws IOException {

        //load MenuButton icon
        ImageView imageView = loadIcon();

        MenuItem menuItem1 = new MenuItem("User 1");
        MenuItem menuItem2 = new MenuItem("User 2");

        //create MenuButton - pass ImageView via constructor
        MenuButton menuButton1 = new MenuButton("Show User", imageView, menuItem1, menuItem2);

        MenuItem menuItem2_1 = new MenuItem("User 1");
        MenuItem menuItem2_2 = new MenuItem("User 2");

        //second example - pass ImageView via setGraphic()
        ImageView imageView2 = loadIcon();
        MenuButton menuButton2 = new MenuButton("Show User", null, menuItem2_1, menuItem2_2);
        menuButton2.setGraphic(imageView2);

        VBox vbox = new VBox(menuButton1, menuButton2);
        Scene scene = new Scene(vbox);
        primaryStage.setScene(scene);
        primaryStage.setWidth (300);
        primaryStage.setHeight(300);
        primaryStage.show();
    }

    private ImageView loadIcon() throws FileNotFoundException {
        FileInputStream input = new FileInputStream("assets/media/iconmonstr-user-1-32.png");
        Image image = new Image(input);
        ImageView imageView = new ImageView(image);
        return imageView;
    }

}

Download

Download the code using the below links:

Number Link
1. Download Example
2. Follow code author
3. Code: Apache 2.0 License