Welcome guys. Learn the JavaFX UI widgets using these code snippets.
Example 1: Tabpane
Let us look at the 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:
TabPaneExample.java
- In your editor or IDE, create a file known as
TabPaneExample.java
. - Then add the following code:
(a). TabPaneExample.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.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
Extend the class
as shown below:
public class TabPaneExample 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) {
TabPane tabPane = new TabPane();
Tab tab1 = new Tab("Planes", new Label("Show all planes available"));
Tab tab2 = new Tab("Cars" , new Label("Show all cars available"));
Tab tab3 = new Tab("Boats" , new Label("Show all boats available"));
tabPane.getTabs().add(tab1);
tabPane.getTabs().add(tab2);
tabPane.getTabs().add(tab3);
tabPane.getSelectionModel().getSelectedItem();
VBox vBox = new VBox(tabPane);
Scene scene = new Scene(vBox);
primaryStage.setScene(scene);
primaryStage.setTitle("JavaFX App");
primaryStage.show();
}
Here is the full code:
package com.jenkov.javafx.tabpane;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TabPaneExample extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
TabPane tabPane = new TabPane();
Tab tab1 = new Tab("Planes", new Label("Show all planes available"));
Tab tab2 = new Tab("Cars" , new Label("Show all cars available"));
Tab tab3 = new Tab("Boats" , new Label("Show all boats available"));
tabPane.getTabs().add(tab1);
tabPane.getTabs().add(tab2);
tabPane.getTabs().add(tab3);
tabPane.getSelectionModel().getSelectedItem();
VBox vBox = new VBox(tabPane);
Scene scene = new Scene(vBox);
primaryStage.setScene(scene);
primaryStage.setTitle("JavaFX App");
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 |