Introduction to JavaFX

Introduction to JavaFX

JavaFX is a graphical user interface toolkit for building desktop and mobile applications. It is based on the Java programming language and provides a rich set of features for creating user interfaces, including:

  • Stage: A Stage represents a top-level window on the screen. It contains a Scene, which is the content of the window.
  • Scene: A Scene is a container that holds the content of a window. It can contain any type of JavaFX node, including controls, shapes, and text.
  • Layout: A Layout is a way of arranging nodes in a Scene. There are many different types of layouts available, such as BorderPane, FlowPane, and GridPane. The choice of layout depends on the specific needs of the application.
  • Control:A Control is a node that allows the user to interact with the application. Examples of Controls include buttons, text fields, and check boxes.
  • Event:An Event is a notification that is sent to a Control when a user interacts with it. For example, a Button event is fired when the user clicks the button.

To create a JavaFX application, you must first create a Stage. You can then create a Scene and add it to the Stage. Finally, you can add Controls to the Scene and arrange them using a Layout.

To listen for events, you can use the `onAction()` method. The `onAction()` method takes a lambda expression as its argument. The lambda expression will be executed when the event occurs.

Here is an example of a simple JavaFX application:


import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;

public class MyApp extends Application {

@Override
public void start(Stage stage) throws Exception {
Button button = new Button("Click Me!");

button.setOnAction(event -> {
  System.out.println("You clicked the button!");
});

Scene scene = new Scene(button);
stage.setTitle("My JavaFX Application");
stage.setScene(scene);
stage.show();

}

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

This application will create a window with a button. When the user clicks the button, the message "You clicked the button!" will be printed to the console.

Stage, Scene, Layout, Control, and Events are the fundamental building blocks of JavaFX applications. By understanding how to use these concepts, you can create a wide variety of JavaFX applications.

Here are some additional benefits of using Stage, Scene, Layout, Control, and Events in JavaFX:

  • Flexibility: JavaFX applications are highly flexible and can be customized to meet the specific needs of the application.
  • Portability: JavaFX applications are portable and can be run on a variety of platforms, including Windows, macOS, and Linux.
  • Performance: JavaFX applications are performant and can be used to create high-performance graphical user interfaces.

Overall, Stage, Scene, Layout, Control, and Events are a powerful set of tools for creating JavaFX applications. By understanding how to use these concepts, you can create a wide variety of flexible, portable, and performant graphical user interfaces.

Exercises

Code for creating a simple stage in javafx.


  package practiceproject;
  import java.io.*;
  import javafx.application.Application;
  import javafx.scene.Scene;
  import javafx.scene.control.Button;
  import javafx.scene.layout.GridPane;
  import javafx.scene.layout.HBox;
  import javafx.stage.Stage;
  public class democlass extends Application{
    
      public static void main(String[] args){
    
        launch();
      }
  
    @Override
    public void start(Stage primaryStage) throws Exception {
      Button bt1 = new Button("Click Me"); //control
      HBox h = new HBox(); //layout
      h.getChildren().add(bt1); //adding control to layout
      Scene sc = new Scene(h); //creating scene and adding layout to scene
      primaryStage.setScene(sc);
      primaryStage.show();		
    }
  } 

HBox layout demo Code in javafx.


package practiceproject;
import java.io.*;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class democlass extends Application{
  
    public static void main(String[] args){
  
      launch();
    }

  @Override
  public void start(Stage primaryStage) throws Exception {
    Button bt1 = new Button("Click Me");
    Button bt2 = new Button("Click Me");
    Button bt3 = new Button("Click Me");
    Button bt4 = new Button("Click Me");
    Button bt5 = new Button("Click Me");
    HBox h = new HBox(); 
    h.getChildren().add(bt1);
    h.getChildren().add(bt2);
    h.getChildren().add(bt3);
    h.getChildren().add(bt4);
    h.getChildren().add(bt5);
    Scene sc = new Scene(h); 
    primaryStage.setScene(sc);
    primaryStage.setWidth(300);
    primaryStage.setHeight(300);
    primaryStage.show();
  }
} 

VBox layout demo Code in javafx


  package practiceproject;
  import java.io.*;
  import javafx.application.Application;
  import javafx.scene.Scene;
  import javafx.scene.control.Button;
  import javafx.scene.layout.GridPane;
  import javafx.scene.layout.HBox;
  import javafx.scene.layout.VBox;
  import javafx.stage.Stage;
  public class democlass extends Application{
    
      public static void main(String[] args){
    
        launch();
      }
  
    @Override
    public void start(Stage primaryStage) throws Exception {
      Button bt1 = new Button("Click Me");
      Button bt2 = new Button("Click Me");
      Button bt3 = new Button("Click Me");
      Button bt4 = new Button("Click Me");
      Button bt5 = new Button("Click Me");
      VBox h = new VBox(); 
      h.getChildren().add(bt1);
      h.getChildren().add(bt2);
      h.getChildren().add(bt3);
      h.getChildren().add(bt4);
      h.getChildren().add(bt5);
      Scene sc = new Scene(h); 
      primaryStage.setScene(sc);
      primaryStage.setWidth(300);
      primaryStage.setHeight(300);
      primaryStage.show();
    }
  } 

Grid Pane layout demo Code in javafx


package practiceproject;
import java.io.*;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class democlass extends Application{
  
    public static void main(String[] args){
  
      launch();
    }

  @Override
  public void start(Stage primaryStage) throws Exception {
    Button bt1 = new Button("Click Me");
    Button bt2 = new Button("Click Me");
    Button bt3 = new Button("Click Me");
    Button bt4 = new Button("Click Me");
    GridPane root = new GridPane();
    root.add(bt1, 0, 0);
    root.add(bt2, 1, 0);
    root.add(bt3, 2, 0);
    Scene sc = new Scene(root); 
    primaryStage.setScene(sc);
    primaryStage.setWidth(300);
    primaryStage.setHeight(300);
    primaryStage.show();
  }
} 

Making Grid Lines visible on a Grid Pane layout demo Code in javafx


package practiceproject;
import java.io.*;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class democlass extends Application{
  
    public static void main(String[] args){
  
      launch();
    }

  @Override
  public void start(Stage primaryStage) throws Exception {
    Button bt1 = new Button("Click Me");
    Button bt2 = new Button("Click Me");
    Button bt3 = new Button("Click Me");
    Button bt4 = new Button("Click Me");
    GridPane root = new GridPane();
    root.add(bt1, 0, 0);
    root.add(bt2, 1, 1);
    root.add(bt3, 2, 2);
    root.setGridLinesVisible(true);
    Scene sc = new Scene(root); 
    primaryStage.setScene(sc);
    primaryStage.setWidth(300);
    primaryStage.setHeight(300);
    primaryStage.show();
  }
} 

Setting VGAP and HGAP on a Grid Pane layout demo Code in javafx


  package practiceproject;
  import java.io.*;
  import javafx.application.Application;
  import javafx.scene.Scene;
  import javafx.scene.control.Button;
  import javafx.scene.layout.GridPane;
  import javafx.scene.layout.HBox;
  import javafx.scene.layout.StackPane;
  import javafx.scene.layout.VBox;
  import javafx.stage.Stage;
  public class democlass extends Application{
    
      public static void main(String[] args){
    
        launch();
      }
  
    @Override
    public void start(Stage primaryStage) throws Exception {
      Button bt1 = new Button("Click Me");
      Button bt2 = new Button("Click Me");
      Button bt3 = new Button("Click Me");
      Button bt4 = new Button("Click Me");
      GridPane root = new GridPane();
      root.add(bt1, 0, 0);
      root.add(bt2, 1, 1);
      root.add(bt3, 2, 2);
      root.setHgap(20);
      root.setVgap(20);
      Scene sc = new Scene(root); 
      primaryStage.setScene(sc);
      primaryStage.setWidth(300);
      primaryStage.setHeight(300);
      primaryStage.show();
    }
  }  
 

flowpane layout demo Code in javafx


package practiceproject;
import java.io.*;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class democlass extends Application{
  
    public static void main(String[] args){
  
      launch();
    }

  @Override
  public void start(Stage primaryStage) throws Exception {
    Button bt1 = new Button("Click Me");
    Button bt2 = new Button("Click Me");
    Button bt3 = new Button("Click Me");
    Button bt4 = new Button("Click Me");
    Button bt5 = new Button("Click Me");
    Button bt6 = new Button("Click Me");
    Button bt7 = new Button("Click Me");
    Button bt8 = new Button("Click Me");
    Button bt9 = new Button("Click Me");
    Button bt10 = new Button("Click Me");
    Button bt11 = new Button("Click Me");
    Button bt12 = new Button("Click Me");
    FlowPane h = new FlowPane(); 
    h.getChildren().add(bt1);
    h.getChildren().add(bt2);
    h.getChildren().add(bt3);
    h.getChildren().add(bt4);
    h.getChildren().add(bt5);
    h.getChildren().add(bt6);
    h.getChildren().add(bt7);
    h.getChildren().add(bt8);
    h.getChildren().add(bt9);
    h.getChildren().add(bt10);
    h.getChildren().add(bt11);
    h.getChildren().add(bt12);
    Scene sc = new Scene(h); 
    primaryStage.setScene(sc);
    primaryStage.setWidth(300);
    primaryStage.setHeight(300);
    primaryStage.show();
  }
} 

BorderPane layout demo Code in javafx.

package demoProject;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import javafx.scene.layout.BorderPane;
public class demoClass extends Application{
  
    public static void main(String[] args){
  
      launch();
    }
  @Override
  public void start(Stage primaryStage) throws Exception {
        Button bt1 = new Button("Click Me");
	    Button bt2 = new Button("Click Me");
	    Button bt3 = new Button("Click Me");
	    BorderPane root	 = new BorderPane();
	    root.setBottom(bt1);
	    root.setTop(bt2);
	    root.setCenter(bt3);
	    Scene sc = new Scene(root); 
	    primaryStage.setScene(sc);
	    primaryStage.setWidth(300);
	    primaryStage.setHeight(300);
	    primaryStage.show();
	  }
	}