JAVA SWING | Graphical User
Interface
FBIT | SEMESTER 02
Overview
Java Swing is a popular GUI (Graphical User Interface) toolkit for Java developers. It provides a
set of components for building desktop applications in Java. It's part of the Java Foundation
Classes (JFC) and provides a comprehensive set of components for creating desktop
applications. Its component-based architecture simplifies GUI development by representing
elements as objects, facilitating the construction of intricate interfaces through component
assembly. With platform independence at its core, GUIs built with Swing seamlessly run across
various platforms supporting Java, eliminating the need for platform-specific modifications and
enhancing accessibility.
Working with Swing
Swing Hierarchy
Figure 1- Java Swing Hierarchy
([Link], 2021)
Swing Experiment
Experiment 1
Let us create a minimal login page using Swing library!
Figure 2- creating a frame
In this code, a JFrame object named f with the title "Login Page" has been created. This JFrame
serves as the main window for your Swing GUI application.
Figure 3- customizing the window
Figure 4- add components to frame
We've successfully added two sets of labels and text fields and a button to our frame. This
creates a simple form-like interface where users can input their username and password finally
pressing login button to submit the credentials.
Figure 5- set frame layout
With the frame's layout set to FlowLayout, components added to the frame will be arranged in a
flow from left to right, wrapping to the next line when they reach the edge of the frame. This
layout manager is useful for simple arrangements of components.
Figure 6- customize frame's properties
We’ve adjusted the size of the frame to be 250 pixels wide and 400 pixels tall, and then made it
visible. Additionally, we’ve set a closing operation feature to exit when close is chosen.
Outcome
Figure 7- Login Form
We’ve successfully created a login form
Experiment 02
Let us now experiment few of the components that are available in Swing
Figure 8- experimenting certain components on a frame
Certainly! Let's go through each component added to the JFrame in the above code:
1. JLabel (JLabel l):
o A label that displays a text string or an image. In your code, it displays the text
"label example".
2. JComboBox (JComboBox cb):
o A drop-down list component that allows the user to select an item from a list of
options. In your code, it's added without any items, but you can populate it with
options later.
3. JButton (JButton b):
o A clickable button that performs an action when clicked. In your code, it displays
the text "button example".
4. JTextField (JTextField t1):
o A single-line text input field where the user can enter text. In your code, it's
initialized with a width of 10 columns, allowing for input of up to 10 characters.
5. JSlider (JSlider js):
o A slider component that allows the user to select a value from a range by
dragging a knob along a track. In your code, it's added without specifying any
range or initial value.
6. JScrollBar (JScrollBar sb):
o A scrollbar component that allows the user to scroll through content that is too
large to be displayed in the visible area. In your code, it's added without
specifying its orientation (horizontal or vertical), so it defaults to a vertical
scrollbar.
Each of these components adds different types of interactivity and functionality to the GUI.
Outcome
Figure 9- output of the frame with certain components
Brainstorming
Q1. Create a Swing GUI Frame for registration page
Instructions:
1. Add fields for Name in full, Address, Mobile number, Date of birth, Email, Password, Confirm
password.
2. Add login option for the users who has already registered.
3. Add Clear button along with Sign up button
Q2: Create a Swing GUI for a simple weather application.
Instructions:
1. Add a text field to enter the city name.
2. Add a button to fetch the weather information for the entered city.
3. Display the weather information, such as temperature, humidity, and weather conditions, in
separate labels or text fields.
4. Include an option to refresh the weather information.
5. Design the GUI to be visually appealing and intuitive for the user.
Note: These questions would not require you to create GUI with logical implementations
References
[Link], 2021. javatpoint. [Online]
Available at: [Link]
[Accessed 22 05 2024].