How JavaFX differs from Swing ? explain steps of creating GUI using JavaFX.

 JavaFX vs. SWING

Swing, AWT, and JavaFX all are a part of JDK and are used to create Graphical User Interface (GUI) with JavaFX being one of the latest entrants in this list. Key differences between JavaFX and Swing are provided below.

Swing

1. Swing is the standard toolkit for Java developers in creating GUI

2. Swing has a more sophisticated set of GUI components

3. Swing is a legacy library that fully features and provides pluggable UI components

4. Swing has a UI component library and acts as a legacy

5. Swing does not have support for customization using CSS and XML

6. With Swing, it is very difficult to create beautiful 3-D applications.


JavaFX

1. JavaFX provides platform support for creating desktop applications.

2. JavaFX has a decent number of UI components available but lesser than what Swing provides.

3. JavaFX has UI components that are still evolving with a more advanced look and feel.

4. JavaFX has several components built over Swing

5. JavaFX has support for customization using CSS and XML 

6. With JavaFX, one can also create beautiful 3-D applications


2nd part

(IN SHORT WE CAN WRITE IN THIS WAY)

Steps for writing JavaFX Program :


1. Extend javafx.application.Application and override start()

import javafx.application.Application;
public class HelloWorld extends Application {
public void start(Stage primaryStage)
{
//Other steps ...
}
}

2. Create a Button

Button btn=new Button("Say, Hello World");


3. Create a layout and add button to it

StackPane root =new StackPane();
root.getChildren().add(btn);

4. Create a Scene

Scene scene =new Scene (root,400,300);

5. Preapare the Stage


primaryStage.setScene(scene);
primaryStage.setTitle("First JavaFX Application");
primaryStage.show();


6. Create an event for the button

button.setOnAction(new Handler());
}
class Handler implements EventHandler<ActionEvent>
{
@Override
public void handle(ActionEvent ae)
{
System.out.println("Hello World");
}
}

7. Create the main method

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

(IN LONG WE CAN WRITE IN THIS WAY)

 Creating Our First JavaFX Application/Steps for writing JavaFX programs

Here, we are creating a simple JavaFX application that prints hello world on the console by clicking the button shown on the stage.

Step 1: Extend javafx.application.Application and override start()

As we have studied earlier the start() method is the starting point of constructing a JavaFX application, therefore, we need to first override the start method of JavaFX.application.Application class. The object of the class javafx.stage.The stage is passed into the start() method therefore import this class and pass its object into the start method. JavaFX.application.The application needs to be imported in order to override the start method.

The code will look like following.

package application;   

import javafx.application.Application;  

import javafx.stage.Stage;  

public class Hello_World extends Application{    

    @Override  

    public void start(Stage primaryStage) throws Exception {  

        // TODO Auto-generated method stub        

    }  

 

Step 2: Create a Button

A button can be created by instantiating the javafx.scene.control.Button class. For this, we have to import this class into our code. Pass the button label text in Button class constructor. The code will look like following.

package application;   

import javafx.application.Application;  

importjavafx.scene.control.Button;  

import javafx.stage.Stage;  

public class Hello_World extends Application{  

    @Override  

    public void start(Stage primaryStage) throws Exception {  

        // TODO Auto-generated method stub  

        Buttonbtn1=newButton("Say, Hello World");    

    }  

}  


Step 3: Create a layout and add a button to it

JavaFX provides a number of layouts. We need to implement one of them in order to visualize the widgets properly. It exists at the top level of the scene graph and can be seen as a root node. All the other nodes (buttons, texts, etc.) need to be added to this layout.

In this application, we have implemented StackPane layout. It can be implemented by instantiating javafx.scene.layout.StackPane class. The code will now look like following.

package application;   

import javafx.application.Application;  

import javafx.scene.control.Button;  

import javafx.stage.Stage;  

import javafx.scene.layout.StackPane;  

public class Hello_World extends Application{    

    @Override  

    public void start(Stage primaryStage) throws Exception {  

        // TODO Auto-generated method stub  

        Button btn1=new Button("Say, Hello World");  

        StackPane root=new StackPane();  

        root.getChildren().add(btn1);  

    } 

}  


Step 4: Create a Scene

The layout needs to be added to a scene. The scene remains at a higher level in the hierarchy of application structure. It can be created by instantiating javafx.scene.Scene class. We need to pass the layout object to the scene class constructor. Our application code will now look like the following.

package application;   

import javafx.application.Application;  

import javafx.scene.Scene;  

import javafx.scene.control.Button;  

import javafx.stage.Stage;  

import javafx.scene.layout.StackPane;  

public class Hello_World extends Application{  

  

    @Override  

    public void start(Stage primaryStage) throws Exception {  

        // TODO Auto-generated method stub  

        Button btn1=new Button("Say, Hello World");  

        StackPane root=new StackPane();  

        root.getChildren().add(btn1);  

        Scene scene=new Scene(root);      

    }  

}  

we can also pass the width and height of the required stage for the scene in the Scene class constructor.


Step 5: Prepare the Stage

javafx.stage.Stage class provides some important methods which are required to be called to set some attributes for the stage. We can set the title of the stage. We also need to call show() method without which, the stage won't be shown. Lets look at the code which describes how can be prepare the stage for the application.

package application;   

import javafx.application.Application;  

import javafx.scene.Scene;  

import javafx.scene.control.Button;  

import javafx.stage.Stage;  

import javafx.scene.layout.StackPane;  

public class Hello_World extends Application{  

    @Override  

    public void start(Stage primaryStage) throws Exception {  

        // TODO Auto-generated method stub  

        Button btn1=new Button("Say, Hello World");  

        StackPane root=new StackPane();  

        root.getChildren().add(btn1);  

        Scene scene=new Scene(root);      

        primaryStage.setScene(scene);  

        primaryStage.setTitle("First JavaFX Application");  

        primaryStage.show();  

    }  

}  


Step 6: Create an event for the button

As our application prints hello world for an event on the button. We need to create an event for the button. For this purpose, call setOnAction() on the button and define an anonymous class Event Handler as a parameter to the method.

Inside this anonymous class, define a method handle() which contains the code for how the event is handled. In our case, it is printing hello world on the console.

package application;   

import javafx.application.Application;  

import javafx.event.ActionEvent;  

import javafx.event.EventHandler;  

import javafx.scene.Scene;  

import javafx.scene.control.Button;  

import javafx.stage.Stage;  

import javafx.scene.layout.StackPane;  

public class Hello_World extends Application{  

    @Override  

    publicvoid start(Stage primaryStage) throws Exception {  

        // TODO Auto-generated method stub  

        Button btn1=new Button("Say, Hello World");  

        btn1.setOnAction(new EventHandler<ActionEvent>() { 

            @Override  

            publicvoid handle(ActionEvent arg0) {  

                // TODO Auto-generated method stub  

                System.out.println("hello world");  

            }  

        });  

        StackPane root=new StackPane();  

        root.getChildren().add(btn1);  

        Scene scene=new Scene(root,600,400);      

        primaryStage.setScene(scene);  

        primaryStage.setTitle("First JavaFX Application");  

        primaryStage.show();  

    }    

}  


Step 7: Create the main method

Till now, we have configured all the necessary things which are required to develop a basic JavaFX application but this application is still incomplete. We have not created the main method yet. Hence, at the last, we need to create a main method in which we will launch the application i.e. will call launch() method and pass the command line arguments (args) to it. The code will now look like the following.

package application;   

import javafx.application.Application;  

import javafx.event.ActionEvent;  

import javafx.event.EventHandler;  

import javafx.scene.Scene;  

import javafx.scene.control.Button;  

import javafx.stage.Stage;  

import javafx.scene.layout.StackPane;  

public class Hello_World extends Application{  

    @Override  

    public void start(Stage primaryStage) throws Exception {  

        // TODO Auto-generated method stub  

        Button btn1=new Button("Say, Hello World");  

        btn1.setOnAction(new EventHandler<ActionEvent>() {  

            @Override  

            public void handle(ActionEvent arg0) {  

                // TODO Auto-generated method stub  

                System.out.println("hello world");  

            }  

        });  

        StackPane root=new StackPane();  

        root.getChildren().add(btn1);  

        Scene scene=new Scene(root,600,400);      

        primaryStage.setTitle("First JavaFX Application");  

        primaryStage.setScene(scene);  

        primaryStage.show();  

    }  

    publicstaticvoid main (String[] args)  

    {  

        launch(args);  

    }    

}  


//Example: Program to demonstrate Steps of Writing JavaFX Programs
import javafx.application.Application; 

import javafx.scene.control.Button;

import javafx.stage.Stage;

import javafx.scene. Scene;

import javafx.scene.layout.StackPane; public class Helloworld extends Application //Step 1

{ public void start (Stage primaryStage) throws Exception 

//Step 1 Contd..
{

//Step 2
Buttonbtn1=new Button("Say, Hello World");

//Step 3
StackPane root-new StackPane(); root.getChildren().add(btn1);

//Step 4
Scene scene-new Scene (root, 400, 300);

//Step 5
primaryStage.setScene (scene);
primaryStage.setTitle ("First JavaFX Application");
primaryStage.show();

//Step 6
btn1.setOnAction(new Handler());
}
class Handler implements EventHandler<ActionEvent> //Step 6 Contd..
{
@Override
public void handle (ActionEvent ae)
{
System.out.println("hello world");
}
}

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

Comments

Popular posts from this blog

What are the key challenges facing in Software Engineering? Explain.

EDI layered architecture

Pure Versus Partial EC