askIT

Java JavaFx: Creating a stage (window) programmatically

document-save.png Download Java javafx_creating_a_stage_window code

What is JavaFx?

javafx.png JavaFX is a software platform for creating and delivering desktop applications, as well as rich Internet applications (RIAs) that can run across a wide variety of devices. JavaFX is intended to replace Swing as the standard GUI library for Java SE, but both will be included for the foreseeable future. JavaFX has support for desktop computers and web browsers on Microsoft Windows, Linux, and macOS.


Stage

The window in JavaFx is called a stage.


Scene

Everything inside a Stage (window) is called a scene.


Creating a JavaFx project

1. Create a normal Java program.

empty_project.png

2. Let your class extend Application.

package com.nettport.coffeecorner;

import javafx.application.Application;

public class Main extends Application {

    public static void main(String[] args) {
        // write your code here
    }
}

3. Let your main method call the method launch(args):

package com.nettport.coffeecorner;

import javafx.application.Application;

public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }
}

4. Implement the start method:

package com.nettport.coffeecorner;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("My program!");

        StackPane root = new StackPane();
        primaryStage.setScene(new Scene(root, 1024, 768));
        primaryStage.show();
    }
}

Now hit the run button to lunch the application. intellij_run.png

javafx_program.png

5. You can also add a button to your program:

package com.nettport.coffeecorner;


import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("My program");

        Button btn = new Button();
        btn.setText("How many programmers does it take to screw in a light bulb?");
        btn.setOnAction(new EventHandler() {

            @Override
            public void handle(ActionEvent event) {
                btn.setText("None. Its a hardware problem.");
            }
        });


        StackPane root = new StackPane();
        root.getChildren().add(btn);
        primaryStage.setScene(new Scene(root, 1024, 768));
        primaryStage.show();
    }
}

intellij_javafx_button.png

The setOnAction method changes the text of the button when it's pressed.

intellij_javafx_button_pressed.png

Add comment

avatar_blank_60.png

Comment:

Name:

Email:

Comments

No comments yet. You can be the first one to comment. Just write your comment in the form and click on the submit button.