Monday, October 14, 2019

Basic JavaFX projects (Part 1)

This is the first of some basic JavaFX example applications.

JavaFx example with normal window controls.

This example is a simple application that includes some standard controls such as Labels and Buttons.  It creates two windows, places several text Labels in one and adds a Button that will remove one of the text Labels.

Clone this repository into Eclipse as was specified in a previous post. (Git Repository view, clone a Git Repository).  The URI is https://github.com/cajanssen/TwoWindowLabelList.git

After the project is created, as before (previous post) do some JavaFX project specific configuration.  Add the already created user library and modify the Run configuration.

Javadocs for JavaFX classes: https://openjfx.io/javadoc/13/allclasses-index.html

Application code:


package jansproj.basicfx;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class TwoWindowLabelList extends Application
{

    public void init() throws Exception
    {
        System.out.println("init called");
    }

    @Override
    public void start(Stage stage) throws Exception
    {
        stage.setTitle("JavaFX ThreeApp");

        FlowPane root = new FlowPane(Orientation.VERTICAL);

        Label label1 = new Label("First Label");
        Label label2 = new Label("Second Label");
        Label label3 = new Label("Third Label");
        Label label4 = new Label("Fourth Label");
        Label label5 = new Label("Fifth Label");
        Label label6 = new Label("Sixth Label");
        Button button1 = new Button("Remove");
        button1.setOnAction(new EventHandler<ActionEvent>()
            {
                public void handle(ActionEvent event)
                   {
                       System.out.println("button pressed");
                       System.out.println(event.getTarget().toString());
                       root.getChildren().remove(label1);
                   }
               });

        root.getChildren().add(button1);
        root.getChildren().add(label1);
        root.getChildren().add(label2);
        root.getChildren().add(label3);
        root.getChildren().addAll(label4,label5,label6);
        Scene scene = new Scene(root, 400, 300);

        stage.setScene(scene);
        stage.show();

        FlowPane secondRoot = new FlowPane(Orientation.VERTICAL);
        Label secondLabel = new Label("Second Window Label");
        secondRoot.getChildren().add(secondLabel);
        Scene secondScene = new Scene(secondRoot, 200, 400);
        Stage secondStage = new Stage();
        secondStage.setScene(secondScene);
        secondStage.setX(10);
        secondStage.setY(10);
        secondStage.show();
    }

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

The first Stage (essentially the window that the application is contained within) is created by the system and handed to the application.  An additional window can be created by the application by creating an additional Stage, along with a corresponding Scene and content graph.  The Scene needs a Node of type Parent as the root of the content graph.  These items are kind of boilerplate.  Note that the root(s) in this application are of type FlowPane, whereas in the previous JavaFX example it was a simple Group object.  The FlowPane provides for some layout to be done.

The Button has an anonymous inner class for its event handler, a fairly common technique.  When pressed, it prints to the command line and removes one of the Labels added to the FlowPane, although it only can do the removal once since after the first time there is no matching object in the List.

No comments:

Post a Comment