Java Swing
Creating desktop applications with traditional GUI components
🖼️ What is Java Swing?
Java Swing is a mature GUI toolkit for building desktop applications. It provides a rich set of lightweight components that work across different platforms with consistent look and feel.
// Simple Swing Application
import javax.swing.*;
public class HelloSwing {
public static void main(String[] args) {
JFrame frame = new JFrame("Hello Swing");
JLabel label = new JLabel("Welcome to Swing!");
frame.add(label);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Output:
Welcome to Swing!
Key Swing Concepts
JFrame
Main window container
JFrame frame = new JFrame("Title");
frame.setSize(400, 300);
Components
UI elements like buttons, labels
JButton btn = new JButton("Click");
JLabel lbl = new JLabel("Text");
Layout Managers
Control component positioning
frame.setLayout(new FlowLayout());
frame.setLayout(new BorderLayout());
Event Handling
Respond to user actions
btn.addActionListener(e ->
System.out.println("Clicked!"));
🔹 Basic Swing Components
Essential components for building Swing applications:
import javax.swing.*;
import java.awt.*;
public class SwingComponents {
public static void main(String[] args) {
JFrame frame = new JFrame("Swing Components");
// Text components
JLabel label = new JLabel("Enter your name:");
JTextField textField = new JTextField(20);
JTextArea textArea = new JTextArea(5, 20);
// Buttons
JButton button = new JButton("Submit");
JCheckBox checkBox = new JCheckBox("Remember me");
JRadioButton radio1 = new JRadioButton("Option 1");
JRadioButton radio2 = new JRadioButton("Option 2");
// Group radio buttons
ButtonGroup group = new ButtonGroup();
group.add(radio1);
group.add(radio2);
// Lists and combos
String[] items = {"Item 1", "Item 2", "Item 3"};
JComboBox<String> combo = new JComboBox<>(items);
JList<String> list = new JList<>(items);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setVisible(true);
}
}
🔹 Layout Managers
Different ways to arrange components in containers:
// FlowLayout - components in a row
JPanel flowPanel = new JPanel(new FlowLayout());
flowPanel.add(new JButton("Button 1"));
flowPanel.add(new JButton("Button 2"));
// BorderLayout - five regions
JPanel borderPanel = new JPanel(new BorderLayout());
borderPanel.add(new JButton("North"), BorderLayout.NORTH);
borderPanel.add(new JButton("Center"), BorderLayout.CENTER);
borderPanel.add(new JButton("South"), BorderLayout.SOUTH);
// GridLayout - rows and columns
JPanel gridPanel = new JPanel(new GridLayout(2, 3));
for (int i = 1; i <= 6; i++) {
gridPanel.add(new JButton("Button " + i));
}
// BoxLayout - vertical or horizontal
JPanel boxPanel = new JPanel();
boxPanel.setLayout(new BoxLayout(boxPanel, BoxLayout.Y_AXIS));
boxPanel.add(new JButton("Top"));
boxPanel.add(new JButton("Middle"));
boxPanel.add(new JButton("Bottom"));
🔹 Event Handling
Responding to user interactions in Swing:
import javax.swing.*;
import java.awt.event.*;
public class EventExample extends JFrame {
private JTextField textField;
private JLabel resultLabel;
public EventExample() {
setTitle("Event Handling Example");
setLayout(new FlowLayout());
textField = new JTextField(15);
JButton button = new JButton("Click Me");
resultLabel = new JLabel("Result will appear here");
// Action listener for button
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = textField.getText();
resultLabel.setText("You entered: " + text);
}
});
// Lambda expression (shorter way)
button.addActionListener(e -> {
String text = textField.getText();
resultLabel.setText("You entered: " + text);
});
add(new JLabel("Enter text:"));
add(textField);
add(button);
add(resultLabel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 150);
setVisible(true);
}
public static void main(String[] args) {
new EventExample();
}
}