📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
What Is JavaFX?
Features of JavaFX
- Written in Java − The JavaFX library is written in Java and is available for the languages that can be executed on a JVM, which include − Java, Groovy and JRuby. These JavaFX applications are also platform-independent.
- Using FXML - FXML is an XML-based markup language that enables developers to create a user interface (UI) in a JavaFX application separately from implementing the application logic.
- JavaFX Scene Builder - To help developers build the layout of their applications, JavaFX provides a design tool called the JavaFX Scene Builder. You drag and drop UI components to a JavaFX Content pane, and the tool generates the FXML code that can be used in an IDE such as NetBeans or Eclipse.
- Swing Interoperability − In a JavaFX application, you can embed Swing content using the Swing Node class. Similarly, you can update the existing Swing applications with JavaFX features like embedded web content and rich graphics media.
- Built-in UI controls − JavaFX library caters UI controls using which we can develop a full-featured application.
- CSS Support - Cascading style sheets contain style definitions that control the look of UI elements. The usage of CSS in JavaFX applications is similar to the usage of CSS in HTML. With CSS, you can easily customize and develop themes for JavaFX controls and scene graph objects.
- Canvas and Printing API − JavaFX provides Canvas, an immediate mode style of rendering API. Within the package javafx.scene.canvas it holds a set of classes for canvas, using which we can draw directly within an area of the JavaFX scene. JavaFX also provides classes for Printing purposes in the package javafx.print.
- Rich set of API’s − JavaFX library provides a rich set of API’s to develop GUI applications, 2D and 3D graphics, etc.
- Integrated Graphics library − JavaFX provides classes for 2d and 3d graphics.
- Graphics pipeline − JavaFX supports graphics based on the Hardware-accelerated graphics pipeline known as Prism. When used with a supported Graphic Card or GPU it offers smooth graphics. In case the system does not support graphic card then prism defaults to the software rendering stack.
Creating Hello World JavaFX application
The JavaFX Application Class
package com.javaguides.javafx.helloworld;
import javafx.application.Application;
public class HelloWorld extends Application {
}
Implementing start() Method
package com.javaguides.javafx.helloworld;
import javafx.application.Application;
import javafx.stage.Stage;
public class HelloWorld extends Application {
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");
primaryStage.show();
}
}
Adding a main() Method
package com.javaguides.javafx.helloworld;
import javafx.application.Application;
import javafx.stage.Stage;
public class HelloWorld extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");
primaryStage.show();
}
}
Adding a Scene to Stage
package com.javaguides.javafx.helloworld;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;
public class HelloWorld extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("My First JavaFX App");
Label label = new Label("Hello World, JavaFX !");
label.setAlignment(Pos.CENTER);
Scene scene = new Scene(label, 400, 200);
primaryStage.setScene(scene);
primaryStage.show();
}
}
The lifecycle of a JavaFX application
2. It calls the init() method of the Application class.
3. It calls the start() method.
- The app calls Platform.exit()
- The last window of the app is closed.
package com.javaguides.javafx.helloworld;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;
public class HelloWorldApp extends Application {
/**
* The application initialization method.
*/
@Override
public void init() throws Exception {
super.init();
System.out.println("Inside init() method! Perform necessary initializations here.");
}
/**
* The main entry point for all JavaFX applications.
* The start method is called after the init method has returned,and
* after the system is ready for the application to begin running.
*/
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("My First JavaFX App");
Label label = new Label("Hello World, JavaFX !");
label.setAlignment(Pos.CENTER);
Scene scene = new Scene(label, 400, 200);
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* The main entry point for all JavaFX applications.
* The start method is called after the init method has returned,
* and after the system is ready for the application to begin running.
*/
@Override
public void stop() throws Exception {
super.stop();
System.out.println("Inside stop() method! Destroy resources. Perform Cleanup.");
}
/**
* main method to lunch the application with parameters
* @param args
*/
public static void main(String[] args) {
launch(args);
}
}
Comments
Post a Comment
Leave Comment