Welcome guys. Learn the JavaFX UI widgets using these code snippets.
Example 1: Splitpane
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:
SplitPaneExample.java
- In your editor or IDE, create a file known as
SplitPaneExample.java
. - Then add the following code:
(a). SplitPaneExample.java
We will need functionalities injected into this class via ready made classes. But first we have to import them. Let’s go ahead and do just that:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.SplitPane;
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 SplitPaneExample extends Application {
Our class
will have the following methods:
void main(String[] args)
void start(Stage primaryStage)
We will add a main method to our Java class as shown below:
public static void main(String[] args) {
Here is the full code:
package com.jenkov.javafx.splitpane;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.SplitPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class SplitPaneExample extends Application {
public static void main(String[] args) {
launch(args);
}
public void start(Stage primaryStage) {
SplitPane splitPane = new SplitPane();
VBox leftControl = new VBox(new Label("Left Control"));
VBox midControl = new VBox(new Label("Mid Control"));
VBox rightControl = new VBox(new Label("Right Control"));
splitPane.getItems().addAll(leftControl, midControl, rightControl);
Scene scene = new Scene(splitPane);
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 |