This tutorial discusses the basics of GUI (Graphical User Interface) programming and Java Foundation Classes or JFC in Java:
In our previous tutorials, we have covered topics ranging from the basics of Java-like data types, variables, decision-making, and loop constructs, etc. to arrays, collections, and methods in Java.
We have also discussed the concept of multithreading and exceptions. After that, we learned the Java OOPS support. Using all these concepts, we can develop high-level applications in Java that are reusable and robust as well as self-contained.
=> Check Here To See A-Z Of Java Training Tutorials.
In this tutorial, we will begin our discussion on the graphical development of applications in Java. Like all other concepts, Java provides numerous reusable graphic classes and APIS so that we can develop our Graphical User Interface (GUI).
What You Will Learn:
What Is A Graphical User Interface In Java
Graphical User Interface or simply called “GUI” is a general term used in the software world. A GUI represents an application that has a visual display for the user with easy to use controls. A GUI generally consists of graphical components like windows, frames, buttons, labels, etc.
We can use these components to interact with the system or even the outside world. Java provides many APIs and reusable classes using which we can develop GUI applications. One of the oldest kits provided by Java is ‘Abstract Windowing ToolKit” or AWT. All newer APIs or components like Swing; JavaFX, etc. are based on this AWT.
Console programs that we have written so far are equally important but we should know that all the latest modern desktop applications are graphically enabled. This means that they have a ready-to-use GUI which makes it easier for the end-user to use the application with the click of a mouse.
Note that writing GUI is not as easy as drawing some shapes or including images. The GUI contains a sequence of activities that also trigger some events that in turn execute some actions on invoking a component or part of a component like by clicking a button we trigger some actions.
So a GUI application is a framework consisting of graphical components & events that can be triggered on these components and the actions that execute as a result of events trigger.
Frameworks usually provide precompiled reusable classes and components that we can drag and drop in the drawing area and then associate the events and actions with these components.
Java provides the following main frameworks.
Java provides the following frameworks for GUI programming:
- Abstract Windowing Toolkit: This is the oldest framework in Java and it was first introduced in JDK 1.0. Most of the AWT components are now outdated and are replaced by Java swing components.
- Swing API: This is a set of graphical libraries developed on top of the AWT framework and is a part of Java Foundation Classes (JFC). Swing has modular architecture wherein we can use plug-and-play for the components.
- JavaFX: The latest framework is available from Java 8 onwards.
There are various third-party graphical frameworks like Eclipse’s Standard Widget Toolkit (SWT) or Google Web Toolkit (GWT) used on Android.
Java Foundation Classes (JFC)
Java provides a set of features and functionality for developing graphical user interfaces or GUIs. This set of features is known as Java Foundation Classes or JFC.
Using JFC we can add rich graphical functionality to applications and also make them more interactive. JFC contains a set of graphical components that can be easily plugged into our application and programmed as per our requirements. But programming these components usually takes only a few lines of code.
JFC contains classes from java.awt and javax.swing packages. Apart from these, it also contains classes related to Java 3D, Java 2D, Java Accessibility, etc. JFC is in sync with Java’s object and components model and thus is easy to use.
The following diagram summarizes various components in JFC.
As shown above, JFC contains AWT as well as Swing classes. We will discuss each of these as we proceed with Java AWT and Java Swing tutorials. JFC also provides various methods that we can use within our application.
Some of the commonly used methods are:
|public void add(Component c)||Adds a component to the parent component invoking the method.|
|public void setSize(int width,int height)||Sets the size of a component with a specified width and height.|
|public void setLayout(LayoutManager m)||Set the specified layout manager for this component.|
|public void setVisible(boolean b)||Set component visibility as per the boolean value. By default the visibility is false.|
Now let’s move on to discuss the GUI components at a general level. Then in our subsequent tutorials, we will discuss the AWTand Swing packages and their components specifically.
We know that the parent container of any GUI application is a Window that may contain one or more frames.
A GUI generally includes user interface elements or the elements that are displayed as a part of the GUI in the application.
These window components are as follows:
- Informational Elements: The elements or components that provide us information like labels, icons, banners, or message dialogs.
- Input Controls: Text fields, buttons, dropdown lists, checkboxes, radio buttons, etc. are the input controls that allow us to read data from the user.
- Navigational Components: Components like the menu, sidebars, breadcrumbs, etc. help us to navigate through the application.
Let us now describe some important components in the GUI.
Frame In Java
A frame is a graphical window visible on the screen. A frame can be viewed as a component but as it forms a physical window that we can see on the screen, it is treated differently than the other components. A frame can have other graphical components like buttons, text fields, etc. and panels too.
Frames also have methods that are not supported by other components. “java.awt.Frame” represents a class in AWT while JFrame is the class contained in the Java Swing package that represents a Frame.
So any GUI application will begin by constructing a JFrame object which forms the main window. Once the JFrame object is created, we can set its visible property to true by calling setVisible (‘true’). Then we can drag and drop the other components in this frame.
All the GUI components including frames have some common properties as they are a part of a common inheritance hierarchy shown above. So we can say that there is a powerful case of code sharing in the Java Swing package.
The following figure shows a typical frame window in the GUI editor.
As we can see from the above figure, the outermost window in the design editor (marked with a smaller red arrow) is the frame. Inside this frame, we have various other components or child components of the frame.
This is evident from the panel on the left-hand side which shows a tree structure of the components. As marked by a bigger red arrow, JFrame is the root component and then all the other components are its child components.
JPanel In Java
JPanel container is a part of the Swing package in Java. JPanel stores a group of components. JPanel organized the components using the layout set by it. Various layouts can help JPanel to better organize the components.
JPanel is considered as a basic building block for component layout in a frame or inside other panels. A GUI application usually has Frame as the main window and one or more panels inside it to hold and organize various components.
The following image shows a simple example of a panel inside a frame.
As shown above, a Panel container is embedded in a Frame window container. We can have one or more panels or even a panel inside a panel. Then we can apply any layout manager to the panel that will organize the components or elements inside the panel. The default layout manager for JPanel is “FlowLayout”.
GUI Layouts In Java
Java container uses an automatic layout of components on the screen. But the size and positions of the components are taken care of by the ‘layout manager’. The layout manager is set for a container at design time. It can also be changed dynamically.
Common layouts used in Java GUI are:
The border layout divides the component into 5 areas as shown below.
Out of these, 4 areas namely “North”, “South”, “East” and “West” are non-resizable. The area “Center” is resizable. Note that each area has a limit of only one component and panels can be used to hold multiple components.
#2) FlowLayout: This layout is used to hold multiple components. Multiple components are arranged horizontally and they wrap if the container width is exhausted. Also, components are displayed in left-to-right order.
#3) BoxLayout: Using the box layout, the components are arranged either vertically or horizontally. This arrangement of the box layout is independent of the container size. The components in the container are set so that they fill the container and have identical sizes.
These were some of the important and common components that we have in GUI development. In our subsequent tutorials on AWT and Swing, we will go into the details of the other components provided by these packages.
Frequently Asked Questions
Q #1) What is GUI programming?
Answer: The programs developed using graphical components that interact with the computer using the pointing device and is event-driven i.e. it performs some actions when certain events are triggered, and is called GUI programming.
A GUI programming uses GUI frameworks wherein we can directly plug and use graphical components.
Q #2) What GUI to use with Java?
Answer: Java supports some of the best frameworks that we can use to develop GUI applications as shown below:
- Swing GUI Java.
- AWT: Abstract Windowing Toolkit.
- SWT- Standard Widget Toolkit.
- Apache Pivot.
Q #3) What are the advantages of a GUI?
Answer: GUI program makes applications interactive, intuitive, and easier to use and learn. As they provide the user with graphical visuals, the actions have more effect and thus we get immediate visual feedback about the application.
Q #4) What is JavaFX API?
Answer: JavaFX is the latest Java library written in the native Java code and contains classes and interfaces. JavaFX can be used to design GUI applications in Java versions later than Java 8.
Q #5) Is Java good for GUI?
Answer: Although Java has more tools to develop GUI applications along with the best support, it is not as fast and as efficient as the MFC library of C++ that has almost all the features to develop advanced GUI applications.
In this tutorial, we explored GUI programming in Java. We introduced some of the important concepts of GUI programming in Java and also saw an overview of the GUI frameworks supported by Java. We discussed the overview of the JFC library.
=> Check ALL Java Tutorials Here.