- swing is a part of Java’s JFC(Java Foundation Classes) and provides a rich set of gui components for building desktop applications
- lightweight
- platform-independant
- highly customizable
Swing Components
- JFrame - top level container for a swing application
- JPanel - a generic container for grouping other containers
- JButton - a clickable button for user interactions
- JLabel - displays text or images
- JTextField - allows user input of a single line
- JTextArea - supports multi-line text input
- JCheckBox - a textbox for selecting options
- JRadioButton - a radio button for mutually exclusive selections
- JComboBox - a dropdown menu for selecting one option from a list
- JTable - displays tabular data
- JScrollPane - adds scrollbar to components like textarea or tables
- JMenu - for creating menu’s
- JMenuBar - for modal or non-modal dialog boxes.
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Swing App");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
JPanel panel = new JPanel()
JLabel label = new JLabel("Enter Name: ");
JTextField textField = new JTextField(20);
JButton button = new JButton("Submit");
panel.add(label);
panel.add(textField);
panel.add(button);
frame.add(panel);
frame.setVisible(true);
}
}
Look and feel
the look and feel defines the appearance and behavior of swing components. Swing components supports pluggable look and feel, allowing you to changethe visual style of the application.
common look and feel options are -
- metal
- nimbus
- windows
- motif
- system look and feel
public class Main {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
} catch(Exception e) {
e.printStackTrace();
}
JFrame frame = new JFrame("Nimbus L&F Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
JPanel panel = new JPanel();
panel.add(new JButton("Click Me"));
panel.add(new JLabel("Nimbus Look & Feel"));
frame.add(panel);
frame.setVisible(true);
}
}
Event listeners
swing uses an event-driver model where components generate events and event listeners handle them.
common event listeners -
- ActionListener - Handles actions like button clicks
- MouseListener - Responds to mouse events
- KeyListener - Handles keyboard input
- WindowListener - Manages window events
- ItenListener - handles changes in components like checkboxes or combo boxes.
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
JPanel panel = new JPanel();
JButton button = new JButton("Click Me");
JLabel label = new JLabel("No clicks yet");
button.addActionListener(e -> label.setText("Button Clicked!"));
panel.add(button);
panel.add(label);
frame.add(panel);
frame.setVisible(true);
}
}
Concurrency in Swing
- swing is single threaded and all gui updates must occur on the event dispatch thread(EDT) to avoid thread safety issues.
- Long running tasks are offloaded to worker threads to prevent GUI from freezing
import javax.swing.*;
import java.awt.*;
import java.util.List;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("SwingWorker Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
JPanel panel = new JPanel();
JLabel label = new JLabel("Status: Idle");
JButton startButton = new JButton("Start Task");
startButton.addActionListener(e -> {
SwingWorker<Void, String> worker = new SwingWorker<>() {
@Override
protected Void doInBackground() throws Exception {
for (int i = 1; i <= 5; i++) {
Thread.sleep(1000); // Simulate long task
publish("Processing step " + i);
}
return null;
}
@Override
protected void process(List<String> chunks) {
label.setText(chunks.get(chunks.size() - 1));
}
@Override
protected void done() {
label.setText("Task Completed!");
}
};
worker.execute();
});
panel.add(startButton);
panel.add(label);
frame.add(panel);
frame.setVisible(true);
}
}