These are simple step by step code snippets to allow you learn JavaFX.
Example 1: Choicebox
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:
ChoiceBoxExample.java
- In your editor or IDE, create a file known as
ChoiceBoxExample.java
. - Then add the following code:
(a). ChoiceBoxExample.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.ChoiceBox;
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 ChoiceBoxExample 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) {
ChoiceBox choiceBox = new ChoiceBox();
choiceBox.getItems().add("Choice 0");
choiceBox.getItems().add("Choice 1");
choiceBox.getItems().add("Choice 2");
choiceBox.setOnAction((event) -> {
int selectedIndex = choiceBox.getSelectionModel().getSelectedIndex();
Object selectedItem = choiceBox.getSelectionModel().getSelectedItem();
System.out.println("Selection made: [" + selectedIndex + "] " + selectedItem);
System.out.println(" ChoiceBox.getValue(): " + choiceBox.getValue());
});
VBox vbox = new VBox(choiceBox);
Scene scene = new Scene(vbox);
primaryStage.setScene(scene);
primaryStage.setWidth(300);
primaryStage.setHeight(300);
primaryStage.setTitle("ChoiceBox Example");
primaryStage.show();
}
Here is the full code:
package com.jenkov.javafx.choicebox;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
* Shows a simple JavaFX Button - and prints a text when the button is clicked.
*/
public class ChoiceBoxExample extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
ChoiceBox choiceBox = new ChoiceBox();
choiceBox.getItems().add("Choice 0");
choiceBox.getItems().add("Choice 1");
choiceBox.getItems().add("Choice 2");
choiceBox.setOnAction((event) -> {
int selectedIndex = choiceBox.getSelectionModel().getSelectedIndex();
Object selectedItem = choiceBox.getSelectionModel().getSelectedItem();
System.out.println("Selection made: [" + selectedIndex + "] " + selectedItem);
System.out.println(" ChoiceBox.getValue(): " + choiceBox.getValue());
});
VBox vbox = new VBox(choiceBox);
Scene scene = new Scene(vbox);
primaryStage.setScene(scene);
primaryStage.setWidth(300);
primaryStage.setHeight(300);
primaryStage.setTitle("ChoiceBox Example");
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 |