We’ve prepared these code snippets to help you learn JavaFX.
Example 1: Scrollpane
Here are the code snippets:
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:
ScrollPaneExample.java
- In your editor or IDE, create a file known as
ScrollPaneExample.java
. - Then add the following code:
(a). ScrollPaneExample.java
We will need to add a bunch of imports in our class:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
We will need to add a bunch of imports in our class:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
The next step is to extend our class
as shown below:
public class ScrollPaneExample extends Application {
Our class
will have the following methods:
void main(String[] args)
void start(Stage primaryStage) throws FileNotFoundException
The next step is to define our main()
method a shown below:
public static void main(String[] args) {
In this particular class
we will be overriding our void start(Stage primaryStage) throws FileNotFoundException
method.
Prepend the code>@Override</code modifier to your method. Then add implementation code as follows:
@Override
public void start(Stage primaryStage) throws FileNotFoundException {
primaryStage.setTitle("JavaFX App");
//String imagePath = "file://E:/video-projects/tutorials/java-switch-expression/assets/aerial-beverage-caffeine-972533.jpg";
//choose the path for some large image on your hard disk here.
String imagePath = "assets/media/abstract-5719221_640.jpg";
ImageView imageView = new ImageView(new Image(new FileInputStream(imagePath)));
//ScrollPane scrollPane = new ScrollPane(imageView);
ScrollPane scrollPane = new ScrollPane();
scrollPane.fitToWidthProperty().set(true);
scrollPane.fitToHeightProperty().set(true);
scrollPane.pannableProperty().set(true);
scrollPane.hbarPolicyProperty().setValue(ScrollPane.ScrollBarPolicy.AS_NEEDED);
//scrollPane.hbarPolicyProperty().setValue(ScrollPane.ScrollBarPolicy.NEVER);
//scrollPane.hbarPolicyProperty().setValue(ScrollPane.ScrollBarPolicy.ALWAYS);
scrollPane.vbarPolicyProperty().setValue(ScrollPane.ScrollBarPolicy.AS_NEEDED);
scrollPane.setContent(imageView);
VBox vBox = new VBox(scrollPane);
//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.scrollpane;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
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;
public class ScrollPaneExample extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws FileNotFoundException {
primaryStage.setTitle("JavaFX App");
//String imagePath = "file://E:/video-projects/tutorials/java-switch-expression/assets/aerial-beverage-caffeine-972533.jpg";
//choose the path for some large image on your hard disk here.
String imagePath = "assets/media/abstract-5719221_640.jpg";
ImageView imageView = new ImageView(new Image(new FileInputStream(imagePath)));
//ScrollPane scrollPane = new ScrollPane(imageView);
ScrollPane scrollPane = new ScrollPane();
scrollPane.fitToWidthProperty().set(true);
scrollPane.fitToHeightProperty().set(true);
scrollPane.pannableProperty().set(true);
scrollPane.hbarPolicyProperty().setValue(ScrollPane.ScrollBarPolicy.AS_NEEDED);
//scrollPane.hbarPolicyProperty().setValue(ScrollPane.ScrollBarPolicy.NEVER);
//scrollPane.hbarPolicyProperty().setValue(ScrollPane.ScrollBarPolicy.ALWAYS);
scrollPane.vbarPolicyProperty().setValue(ScrollPane.ScrollBarPolicy.AS_NEEDED);
scrollPane.setContent(imageView);
VBox vBox = new VBox(scrollPane);
//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 |