Updated April 5, 2023
Introduction to JavaFX GUI
In Java, JavaFX is considered a Graphical User Interface (GUI) toolkit that helps create desktop applications and games. Normally, programmers use Swing and AWT kit (Advanced Windowing Tool kit) libraries for the same. But, after the emergence of JavaFX, programmers rely on this for developing GUI with rich content. The other reason it is used is that Java is still the most common and powerful programming language. Moreover, It supports in all operating systems and devices like Linux, iOS, Windows, Mac, Raspberry Pi, Android and Chromebook.
How does JavaFX GUI Work?
Let us see how GUI works.
Below is the flowchart of the same.
First, create GUI components and render the graphical user interface. Then, check if any input is there. Once this is done, respond to that particular user input. Repeat the process.
To understand this clearly, let us see an example of a mouse click on a particular button. First, the Operating system identifies the mouse click and finds which window is within it. Then, based on that, the program will be notified. After this, the program will be running in a loop, and OS looks at whether the input buffer is full. If the click is found, then the program component will be checked, and if it is in a relevant component, the response will be based on the handler.
JavaFX GUI Component
There are different components in JavaFX GUI such as controls, layouts, charts etc.
1. Controls
They are the components that offer control functionality within the application. The javafx.scene.control is the package that helps in this. The controls are commonly nested within the layout component that handles the layout of controls that are related to each other.
Below are the controls in JavaFX GUI components:
- Spinner
- ProgressBar
- SplitMenuButton
- passwordfield
- SplitPane
- MenuBar
- TableView
- Menu
- TabPane
- ListView
- RadioButton
- TextArea
- Label
- TextField
- ComboBox
- TitledPane
- ColorPicker
- ToggleButton
- ChoiceBox
- Slider
- ToolBar
- CheckBox
- TreeTableView
- Button
- TreeView
- Accordion
2. Layouts
The next one is the layouts that consist of other components within them. It handles the layout nested within it. These are also known as parent components as they include child components, and also due to the fact that layout components are subclasses of javafx.scene.Parent JavaFX class. To make the scene object visible, the layout has to be attached to the scene graph.
Below are the layouts in JavaFX GUI components:
- FlowPane
- StackPane
- VBox
- TilePane
- HBox
- GridPane
- Pane
- AnchorPane
- Region
- TextFlow
- Group
3. Charts
In-built ready-to-use components in JavaFX GUI, the programmer does not want to code the same from scratch. The chart is one such component.
Below are the charts in JavaFX GUI components:
- ScatterChart
- BubbleChart
- StackedAreaChart
- LineChart
- BarChart
- StackedBarChart
- PieChart
- AreaChart
Features of JavaFX GUI
Given below are the features mentioned:
- Java Library: It consists of several classes as well as interfaces that are on Java language.
- Scene Builder: This feature creates a mark-up FXML that can be ported to IDE.
- FXML: Declarative mark-up language based on XML that offers an enhanced GUI to the user.
- Web View: This feature uses WebKitHTML technology for embedding web pages with applications.
- Built-in UI Controls: There are several in-built components that do not depend on the OS. These are enough for developing an application that is full-featured.
- CSS Like Styling: CSS can also be used with JavaFX for the enhancement of GUI view.
- Swing Interoperability: Using the class Swing Node, applications (GUI) can be embedded with a swing. Moreover, current swing applications can also be updated.
- Canvas API: This feature offers methods for directly drawing in an area of the scene in JavaFX.
- Rich Set of APIs: This feature offers a rich API set for developing GUI applications in JavaFX.
- Integrated Graphics Library: This feature offers an integrated class set for sealing with 3D as well as 2D graphics in GUI applications of JavaFX.
- Graphics Pipeline: This feature is based on a prism, which is a graphics rendered pipeline. It provides hardware-accelerated smooth graphics.
- High-Performance Media Engine: This feature supports web multimedia playback on low latency, and it is based on the framework Gstreamer Multimedia.
- Self-contained Application Deployment Model: This feature provides the application resources. Moreover, JavafX and Java runtime private copy is also provided.
Example of JavaFX GUI
A different example is given below:
Code:
//sample program on JavaFX GUI
import javafx.application.Application ;
import javafx.scene.Group ;
import javafx.scene.Scene ;
import javafx.stage.Stage ;
import javafx.scene.text.Font ;
import javafx.scene.text.FontPosture ;
import javafx.scene.text.FontWeight ;
import javafx.scene.text.Text ;
//main class
public class GUISampleProgram extends Application {
@Override
public void start(Stage st) {
//Create a Text object txt
Text txt = new Text();
//Set the text font, weight, posture and size
txt.setFont(Font.font("Arial" , FontWeight.BOLD , FontPosture.ITALIC, 22));
//set the text x and y position
txt.setX(60) ;
txt.setY(120) ;
//Set the text that has to be added
txt.setText("THIS IS THE GUI") ;
//Create a Group object gp
Group gp = new Group(txt) ;
//Create a scene object sc
Scene sc = new Scene( gp, 700, 400) ;
//Set the stage title
st.setTitle(" Sample Output ") ;
//Add a scene sc to the stage st
st.setScene(sc);
//Display the stage contents
st.show();
}
//main method
public static void main(String args[])
{
//launch the application
launch(args);
}
}
Output:
First, import all the packages with necessary GUI components. As the text class of the javafx.scene.text package denotes the JavaFX text node in JavaFX, and a text can be created. Then, set the font, text position and add the text. The font, size of the text also has to be mentioned. After this, create a group object gp and a scene object sc. Then, set the stage title and add scene sc to the stage st.
On executing the code, a GUI is displayed with text.as shown in the screenshot above.
Conclusion
JavaFX is considered a Graphical User Interface (GUI) toolkit that helps create desktop applications and games. In this article, different aspects such as features, components, working and examples of JavaFX GUI are shown in detail.
Recommended Articles
This is a guide to JavaFX GUI. Here we discuss the introduction; how does JavaFX GUI work? Component, features and example. You may also have a look at the following articles to learn more –