We’ve collected these simple code snippets to allow you learn JavaFX.
Study the following code:
Step 1: Create Project
- Open your favorite Java IDE.
- 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:
SplitMenuButtonExample.java
SplitMenuButtonFontExample.java
- In your editor or IDE, create a file known as
SplitMenuButtonExample.java
. - Then add the following code:
(a). SplitMenuButtonExample.java
After creating our class, the first thing is to define imports. Such imports are ready made classes that inject more functionalities into our project.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SplitMenuButton;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
Through inheritance we will be able to derive properties from a parent class
. However we have to extend that parent class
. So we do that using the extends
keyword.
public class SplitMenuButtonExample extends Application {
Our class
will have the following methods:
void main(String[] args)
void start(Stage primaryStage)
For our Java program to run we need a main method. Add it 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) {
primaryStage.setTitle("JavaFX App");
SplitMenuButton button = new SplitMenuButton();
button.setText("Click here!");
button.setOnAction((e) -> {
System.out.println("Button clicked!");
System.out.println(e);
System.out.println();
});
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");
});
button.getItems().addAll(choice1, choice2, choice3);
VBox vBox = new VBox(button);
//HBox hBox = new HBox(button1, button2);
Scene scene = new Scene(vBox, 960, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
Here is the full code:
package com.jenkov.javafx.splitmenubutton;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SplitMenuButton;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class SplitMenuButtonExample extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("JavaFX App");
SplitMenuButton button = new SplitMenuButton();
button.setText("Click here!");
button.setOnAction((e) -> {
System.out.println("Button clicked!");
System.out.println(e);
System.out.println();
});
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");
});
button.getItems().addAll(choice1, choice2, choice3);
VBox vBox = new VBox(button);
//HBox hBox = new HBox(button1, button2);
Scene scene = new Scene(vBox, 960, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
}
- Next create another file known as
SplitMenuButtonFontExample.java
. - And add the following code:
(b). SplitMenuButtonFontExample.java
After creating our class, the first thing is to define imports. Such imports are ready made classes that inject more functionalities into our project.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SplitMenuButton;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
Through inheritance we will be able to derive properties from a parent class
. However we have to extend that parent class
. So we do that using the extends
keyword.
public class SplitMenuButtonFontExample extends Application {
Our class
will have the following methods:
void main(String[] args)
void start(Stage primaryStage)
For our Java program to run we need a main method. Add it 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) {
primaryStage.setTitle("JavaFX App");
SplitMenuButton button = new SplitMenuButton();
button.setText("Click here!");
Font font = Font.font("Courier New", FontWeight.BOLD, 36);
button.setFont(font);
button.setOnAction((e) -> {
System.out.println("Button clicked!");
System.out.println(e);
System.out.println();
});
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");
});
button.getItems().addAll(choice1, choice2, choice3);
VBox vBox = new VBox(button);
//HBox hBox = new HBox(button1, button2);
Scene scene = new Scene(vBox, 960, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
Here is the full code:
package com.jenkov.javafx.splitmenubutton;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SplitMenuButton;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
public class SplitMenuButtonFontExample extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("JavaFX App");
SplitMenuButton button = new SplitMenuButton();
button.setText("Click here!");
Font font = Font.font("Courier New", FontWeight.BOLD, 36);
button.setFont(font);
button.setOnAction((e) -> {
System.out.println("Button clicked!");
System.out.println(e);
System.out.println();
});
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");
});
button.getItems().addAll(choice1, choice2, choice3);
VBox vBox = new VBox(button);
//HBox hBox = new HBox(button1, button2);
Scene scene = new Scene(vBox, 960, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Download
Download the code using the below links:
Number | Link |
---|---|
1. | Download Example |
2. | Follow code author |
3. | Code: Apache 2.0 License |