Day 1: Introduction to Java

icon picker
Day 14: GUI Programming with Swing

Welcome to Day 14 of our Java programming journey! Today, we'll dive into the exciting world of graphical user interface (GUI) programming using Java Swing. Graphical user interfaces provide a user-friendly way to interact with your Java applications, making them more accessible and visually appealing.

Introduction to Swing

Swing is a powerful and flexible GUI toolkit that comes bundled with Java. It allows you to create desktop applications with rich and interactive user interfaces. Swing components are lightweight, which means they don't rely on the native platform's GUI components, making them highly portable across different operating systems.

Swing Components

JFrame

The JFrame class is the foundation for creating windows in Swing applications. It represents the main application window and can contain other Swing components. You can customize the appearance, size, and behavior of a JFrame to suit your application's needs.
Here's how to create a simple JFrame:
javaCopy code
import javax.swing.JFrame;

public class MyFrame extends JFrame {
public MyFrame() {
setTitle("My Swing Application");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

public static void main(String[] args) {
new MyFrame();
}
}

JPanel

A JPanel is a container that can hold other Swing components. You can think of it as a blank canvas within a JFrame where you can add buttons, labels, text fields, and more. JPanel helps you organize and group components within your GUI.

JButton

A JButton is a common component used for user interaction. It's a clickable button that performs an action when clicked. Here's how to create a simple button:
javaCopy code
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MyFrame extends JFrame {
public MyFrame() {
setTitle("My Swing Application");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new JPanel();
JButton button = new JButton("Click Me");

panel.add(button);
add(panel);

setVisible(true);
}

public static void main(String[] args) {
new MyFrame();
}
}

Event Handling

Event handling is crucial in GUI programming. It allows your application to respond to user actions, such as button clicks, mouse movements, or keyboard inputs. In Swing, you can use listeners to handle events. For example, to handle a button click event:
javaCopy code
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Your code to handle the button click event goes here
}
});

Building a Simple GUI Application

Now, let's put everything together to build a simple GUI application. We'll create a JFrame, add a JPanel, and place a JButton on the panel. We'll also handle a button click event.
javaCopy code
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SimpleGUI extends JFrame {
public SimpleGUI() {
setTitle("Simple GUI Application");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new JPanel();
JButton button = new JButton("Click Me");
JLabel label = new JLabel("Hello, Swing!");

button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
label.setText("Button Clicked!");
}
});

panel.add(button);
panel.add(label);
add(panel);

setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new SimpleGUI());
}
}

Code
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class JColorChooserDemo extends JFrame implements ActionListener
{
JButton b;
Container c;

JColorChooserDemo()
{
c = getContentPane();
c.setLayout(new FlowLayout());

b = new JButton("Color Chooser");
b.addActionListener(this);

c.add(b);
}

public void actionPerformed(ActionEvent e)
{
Color initialcolor = Color.RED;
Color color = JColorChooser.showDialog(this, "Select a color", initialcolor);
c.setBackground(color);
}

public static void main(String[] args)
{
JColorChooserDemo jColorChooser = new JColorChooserDemo();
jColorChooser.setSize(400, 400);
jColorChooser.setVisible(true);
jColorChooser.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;


public class ComboBoxDemo extends JPanel
implements ActionListener
{
JLabel picture;

public ComboBoxDemo()
{
super(new BorderLayout());
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.