0% found this document useful (0 votes)
35 views

JAVA FX

Uploaded by

Raja Sekhar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

JAVA FX

Uploaded by

Raja Sekhar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

JavaFX GUI

JavaFX is a Java library and a GUI toolkit designed to develop and facilitate Rich
Internet applications, web applications, and desktop applications.

Features of JavaFX
JavaFX is an open-source framework based on Java, used for advancing rich client
applications.
Java Library
Platform Independent
FXML
Scene Builder
Rich set of APIs
Core Concepts in JavaFX:

Stage: A Stage represents a window in a JavaFX application. Each JavaFX application has at least one
primary stage (the main window), but you can also create additional secondary stages (windows).

Scene: A Scene is the content container that holds all the visual elements (nodes) of the application.
It defines the size, layout, and root of the UI structure.

Nodes: In JavaFX, nodes are elements in the scene graph, such as buttons, labels, text fields, shapes,
images, etc. Every visible element in a JavaFX application is a node.

Layouts: JavaFX provides several layout containers (like VBox, HBox, BorderPane, GridPane, FlowPane,
etc.) to help organize and arrange the placement of nodes within the scene.

Event Handling: JavaFX allows you to handle events (such as mouse clicks, key presses, etc.) and
propagate them through the scene graph.
Architecture of JavaFX

JavaFX API:
javafx.animation
javafx.css
javafx.geometry
javafx.scene
javafx.application
javafx.event
javafx.stage
Scene Graph – A Scene Graph is the starting point of the development of any of the GUI
Applications. In JavaFX, all the GUI Applications are made using a Scene Graph only. The
Scene Graph includes the primitives of the rich internet applications that are known as
nodes. In simple words, a single component in a scene graph is known as a node
Life Cycle of a JavaFX Application
There are in total three life cycle methods of a JavaFX Application class. These methods are –

start() – The start() method is the entry point method of the JavaFX application where all the
graphics code of JavaFX is to be written.
init() – The init() method is an empty method that can be overridden. The init method is a
specila method that is called by the JavaFX runtime to initialize the JavaFX application.
stop() – The stop() method is an empty method that can also be overridden, just like the init()
method. In this method, the user can write the code to halt the application.

Other than these methods, the JavaFX application also implements a static method known as
launch(). This launch() method is used to launch the JavaFX application. As stated earlier, the
launch() method is static, the user should call it from a static method only. Generally, that static
method, which calls the launch() method, is the main() method only.

Whenever a user launches a JavaFX application, there are few actions that are carried out in a
particular manner only. The following is the order given in which a JavaFX application is
launched.
Firstly, an instance of the application class is created.
After that, the init() method is called.
After the init() method, the start() method is called.
After calling the start() method, the launcher waits for the JavaFX application to end and then
calls the stop() method.
Example Program 1
JavaFX UI Controls

1. Label
Used to display a text label that is not editable by the user.

Label label = new Label("This is a Label");

2. Button
A clickable button for performing an action.
Button button = new Button("Click Me");
button.setOnAction(e -> System.out.println("Button clicked!"));

3. TextField
A single-line editable text input.

TextField textField = new TextField();


textField.setPromptText("Enter your name");

4. PasswordField
A single-line text input for entering passwords (hides characters).

PasswordField passwordField = new PasswordField();


passwordField.setPromptText("Enter your password");
5. TextArea
A multi-line editable text input.
TextArea textArea = new TextArea();
textArea.setPromptText("Enter a long message here");

6. CheckBox
A control that allows the user to select or deselect an option.
CheckBox checkBox = new CheckBox("I agree to the terms and conditions");
checkBox.setSelected(true);

7. RadioButton
Used for creating a group of mutually exclusive options.
ToggleGroup group = new ToggleGroup();

RadioButton option1 = new RadioButton("Option 1");


option1.setToggleGroup(group);

RadioButton option2 = new RadioButton("Option 2");


option2.setToggleGroup(group);
JavaFX Shapes
JavaFX provides a rich API to create and display various 2D shapes such as rectangles, circles, ellipses,
polygons, and more. These shapes are part of the javafx.scene.shape package and can be added to a
JavaFX scene just like other UI components.

Common Shapes in JavaFX


1. Line
Draws a straight line between two points.

Line line = new Line(50, 50, 200, 200); // (startX, startY, endX, endY)
line.setStrokeWidth(2);
line.setStroke(javafx.scene.paint.Color.BLUE);
2. Rectangle
Draws a rectangle with a specified width and height.

Rectangle rectangle = new Rectangle(50, 50, 200, 100); // (x, y, width, height)
rectangle.setFill(javafx.scene.paint.Color.LIGHTBLUE);
rectangle.setStroke(javafx.scene.paint.Color.BLACK);
3. Circle
Creates a circle with a given center and radius.

Circle circle = new Circle(150, 150, 50); // (centerX, centerY, radius)


circle.setFill(javafx.scene.paint.Color.YELLOW);
circle.setStroke(javafx.scene.paint.Color.ORANGE);
4. Ellipse
Creates an ellipse with specified radii and center.

Ellipse ellipse = new Ellipse(150, 150, 100, 50); // (centerX, centerY, radiusX, radiusY)
ellipse.setFill(javafx.scene.paint.Color.PINK);
ellipse.setStroke(javafx.scene.paint.Color.RED);
5. Polygon
Creates a closed shape with multiple points.
Polygon polygon = new Polygon();
polygon.getPoints().addAll(
100.0, 50.0,
200.0, 150.0,
50.0, 150.0
);
polygon.setFill(javafx.scene.paint.Color.LIGHTGREEN);
polygon.setStroke(javafx.scene.paint.Color.DARKGREEN);
6. Arc
Draws a portion of an ellipse.

Arc arc = new Arc(150, 150, 100, 100, 45, 180); // (centerX, centerY, radiusX, radiusY,
startAngle, length)
arc.setFill(javafx.scene.paint.Color.LIGHTYELLOW);
arc.setStroke(javafx.scene.paint.Color.ORANGE);
arc.setType(ArcType.ROUND); // Options: ROUND, OPEN, CHORD

You might also like