0% found this document useful (0 votes)
6 views11 pages

JavaFX Tutorial

JavaFX is a Java-based UI toolkit for developing rich client applications that work across multiple platforms, intended to replace Swing. It features FXML for UI design, offers a lightweight and hardware-accelerated framework, and supports various operating systems. The document also provides a basic example of creating a JavaFX application, including setting up a button, layout, scene, and event handling.

Uploaded by

Ravi Shankar Jha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views11 pages

JavaFX Tutorial

JavaFX is a Java-based UI toolkit for developing rich client applications that work across multiple platforms, intended to replace Swing. It features FXML for UI design, offers a lightweight and hardware-accelerated framework, and supports various operating systems. The document also provides a basic example of creating a JavaFX application, including setting up a button, layout, scene, and event handling.

Uploaded by

Ravi Shankar Jha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

JavaFX

What is JavaFX?

JavaFX is a comprehensive set of graphics and media


packages bundled with the Java SE Development Kit (JDK). It
enables developers to design, create, and deploy rich client
applications that work consistently across various platforms,
offering a modern, Java-based UI toolkit for desktop
applications.
A fundamental feature of JavaFX is its declarative language,
FXML. It allows developers to define the user interface (UI) of
their applications using an XML-based syntax. This separation
of UI from application logic simplifies the design and
maintenance of complex interfaces, enhancing development
efficiency.
• JavaFX is intended to replace swing in Java applications as a GUI framework. However, It
provides more functionalities than swing. Like Swing, JavaFX also provides its own
components and doesn't depend upon the operating system. It is lightweight and
hardware accelerated. It supports various operating systems including Windows, Linux
and Mac OS.

• Overall, JavaFX provides a robust toolkit for developing desktop applications with rich and
responsive user interfaces. By leveraging its features and APIs, developers can create
cross-platform applications that deliver a modern and immersive user experience.

History of JavaFX
JavaFX was developed by Chris Oliver. Initially the project was named as Form Follows
Functions (F3) . It is intended to provide the richer functionalities for the GUI
application development. Later, Sun Micro-systems acquired F3 project as JavaFX in
June, 2005.
Sun Micro-systems announces it officially in 2007 at W3 Conference. In October 2008,
JavaFX 1.0 was released. In 2009, ORACLE corporation acquires Sun Micro-Systems and
released JavaFX 1.2.
first JavaFX Application
1.import javafx.application.Application;
2.import javafx.stage.Stage;
3.public class Hello_World extends Application{
4.
5. @Override
6. public void start(Stage primaryStage) throws Exception
{
7. // TODO Auto-generated method stub
8.
9. }
10.
11.}
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.

1.import javafx.application.Application;
2.importjavafx.scene.control.Button;
3.import javafx.stage.Stage;
4.public class Hello_World extends Application{
5.
6. @Override
7. public void start(Stage primaryStage) throws Exception {

8. // TODO Auto-generated method stub


9. Buttonbtn1=newButton("Say, Hello World");
10.
11. }
12.
13.}
Create a layout and add button to it
JavaFX provides the 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. 1.import javafx.application.Application;
2.import javafx.scene.control.Button;
3.import javafx.stage.Stage;
4.import javafx.scene.layout.StackPane;
5.public class Hello_World extends Application{
6.
7. @Override
8. public void start(Stage primaryStage) throws Exception {
9. // TODO Auto-generated method stub
10. Button btn1=new Button("Say, Hello World");
11. StackPane root=new StackPane();
12. root.getChildren().add(btn1);
13.
14. }
15.
16.}
Create a Scene
The layout needs to be added to a scene. Scene remains at the 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 1.import
application code will now look like following.
javafx.application.Application;
2.import javafx.scene.Scene;
3.import javafx.scene.control.Button;
4.import javafx.stage.Stage;
5.import javafx.scene.layout.StackPane;
6.public class Hello_World extends Application{
7.
8. @Override
9. public void start(Stage primaryStage) throws Exception {
10. // TODO Auto-generated method stub
11. Button btn1=new Button("Say, Hello World");
12. StackPane root=new StackPane();
13. root.getChildren().add(btn1);
14. Scene scene=new Scene(root);
15. }
16.
17.}
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.
1.import javafx.application.Application;
2.import javafx.scene.Scene;
3.import javafx.scene.control.Button;
4.import javafx.stage.Stage;
5.import javafx.scene.layout.StackPane;
6.public class Hello_World extends Application{
7.
8. @Override
9. public void start(Stage primaryStage) throws Exception {
10. // TODO Auto-generated method stub
11. Button btn1=new Button("Say, Hello World");
12. StackPane root=new StackPane();
13. root.getChildren().add(btn1);
14. Scene scene=new Scene(root);
15. primaryStage.setScene(scene);
16. primaryStage.setTitle("First JavaFX Application");
17. primaryStage.show();
18. }
19.
20.}
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 a 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.
1.import javafx.application.Application; 17. publicvoid handle(ActionEvent a
2.import javafx.event.ActionEvent; rg0) {
3.import javafx.event.EventHandler; 18. // TODO Auto-
4.import javafx.scene.Scene; generated method stub
5.import javafx.scene.control.Button; 19. System.out.println("hello wor
6.import javafx.stage.Stage; ld");
7.import javafx.scene.layout.StackPane; 20. }
8.public class Hello_World extends Appli 21. });
cation{ 22. StackPane root=new StackPane();
9.
10. @Override 23. root.getChildren().add(btn1);
11. publicvoid start(Stage primaryStage) 24. Scene scene=new Scene(root,600
throws Exception { ,400);
12. // TODO Auto- 25. primaryStage.setScene(scene);
generated method stub 26. primaryStage.setTitle("First JavaFX
13. Button btn1=new Button("Say, He Application");
llo World"); 27. primaryStage.show();
14. btn1.setOnAction(new EventHandl28. }
er<ActionEvent>() { 29.
15. 30.}
16. @Override
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 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 following.
1.import javafx.application.Application; 18. // TODO Auto-generated method stub
2.import javafx.event.ActionEvent; 19. System.out.println("hello world");
3.import javafx.event.EventHandler; 20. }
4.import javafx.scene.Scene; 21. });
5.import javafx.scene.control.Button; 22. StackPane root=new StackPane();
6.import javafx.stage.Stage; 23. root.getChildren().add(btn1);
7.import javafx.scene.layout.StackPane; 24. Scene scene=new Scene(root,600,400);
8.public class Hello_World extends Application{ 25. primaryStage.setTitle("First JavaFX Applicatio
9. ");
10. @Override 26. primaryStage.setScene(scene);
11. public void start(Stage primaryStage) throws E 27. primaryStage.show();
xception { 28. }
12. // TODO Auto-generated method stub 29. publicstaticvoid main (String[] args)
13. Button btn1=new Button("Say, Hello World"); 30. {
31. launch(args);
14. btn1.setOnAction(new EventHandler<ActionE 32. }
vent>() { 33.
15. 34.}
16. @Override

You might also like