17th December 2024

What are Java AWT, Swing and JavaFX?

In this article, there will be information about the AWT, Swing and JavaFX libraries used to create a visual interface with Java, what they do and how they are used.

What is AWT?

AWT (Abstract Window Toolkit) is a package of various components and utility classes used to create visual interfaces with Java. The classes are included in java.awt package. AWT has components such as Button, Label, Checkbox, Choice, List, Container that derive from the java.awt.Component class.

There are Window and Panel components that derive from the Container class. The Frame and Dialog classes, which derive from the Window class, create windows and host components such as Button and Label. The Panel class, on the other hand, is used to host Button and Label components only.

How to Use AWT?

Below we have created a simple window using AWT. AWT has java.awt.event package to handle events such as click(MouseEvent), write(KeyEvent) on components. The package includes ActionEvent, MouseEvent, MouseWheelEvent, KeyEvent, ItemEvent, TextEvent, AdjustmentEvent, WindowEvent, ComponentEvent, ContainerEvent, and FocusEvent events.

AWT Usage
AWT Usage

 

The relevant events that occur in the Button and Frame components are managed below. There are several methods to manage events. In the following example, the implementation of the ActionListener interface in the class and the override method of the method in the WindowAdapter class are included.

Note: Adapter classes also implement these interfaces with empty content. When interfaces are used by using these classes, the necessity of implementing all methods is eliminated.

Frame components
Frame components

 

The AWT package has the Toolkit class to get information about the operating system. Components built with AWT are platform-OS dependent. Therefore, the appearance of the components will be different according to the operating system.

LEARN MORE  What is PowerShell? What Should Be PowerShell Security?
Toolkit Class
Toolkit Class

 

What is Swing?

Swing is a package of various components and classes used to create visual interfaces with java such as AWT. The classes are contained in the “javax.swing” package.

The difference between the swing build and the AWT build is that the Swing components start with “J” and the swing library has more components. The reason for the exit of the Swing library; The AWT library is Java’s non-compliance with the “write once run everywhere” principle, which is called WORA.

The differences in the components in the AWT library according to the operating system have been eliminated with the Swing Look and Feel feature.

How to Use Swing?

Below we have created a simple window using swing. It uses the classes included in the AWT package to handle events happening on swing components.

How to Use Swing?
How to Use Swing?
Swing Components
Swing Components

 

Component Layout

BorderLayout, FlowLayout, GridLayout, CardLayout, GridBagLayout classes in the AWT library can be used for the layout of components with AWT and Swing. In addition, the BoxLayout, GroupLayout, ScrollPaneLayout and SpringLayout classes included in the swing package can also be used.

Although there are multiple classes for component layout, it is useful to use tools such as NetBeans Matisse and Eclipse WindowBuilder for component layout.

What is JavaFX?

JavaFX is a package that includes many components, effects and features used to create visual interfaces such as AWT and Swing. It includes additional components, layout, ready-made graphics components, 2d-3d graphics, audio and video components to the components in the AWT and Swing libraries in JavaFX.

LEARN MORE  How to Disable Enhanced Security Feature in Windows Server 2012?

Unlike AWT and Swing structure, it supports FXML to render components in XML format and CSS for visual formatting. JavaFX was included in the Java platform with Java 8 and was later removed from the platform with Java 11.

How to Use JavaFX?

Below we have created a simple window using JavaFX.

Using JavaFX
Using JavaFX

 

Unlike AWT and Swing libraries, it has the “javafx.event” package to handle events that occur on JavaFX components.

Methods starting with “setOn” are used to handle component events. Unlike AWT and Swing, JavaFX supports FXML and CSS. Thanks to this structure, visual interface design such as HTML in web pages, XML design in CSS mobile applications, XAML structure in the .NET platform can be done.

As with AWT and Swing structures, it will be useful to use the Scene Builder tool, since it will be difficult to place over the code with JavaFX.

javafx.event
javafx.event

 

Leave a Reply

Your email address will not be published. Required fields are marked *