Skip to main content

Java Swing

Introduction to Java Swing

Java Swing is a lightweight Graphical User Interface (GUI) toolkit that includes a rich set of widgets. It is part of Java Foundation Classes (JFC) and includes several packages for developing rich desktop applications in Java.

Overview

Java Swing provides a native look and feel that emulates the look and feel of several platforms, and also supports a pluggable look and feel that allows applications to have a look and feel that is unrelated to the underlying platform.

Getting Started with Java Swing

To get started with Java Swing, you need to import javax.swing package in your Java code.

import javax.swing.*;

This will import all the Swing components for GUI programming.

Creating a Simple Frame

A frame in Java Swing is a window that can have a title bar, menu bar, minimize and maximize button, and close button.

import javax.swing.*;

public class MyFrame {
JFrame frame;
public MyFrame(){
frame = new JFrame();
frame.setTitle("My First Swing Frame");
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

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

In the above code, we have created a JFrame and set its title, size, default close operation and visibility.

Adding Components to a Frame

Swing provides a rich set of components like buttons, labels, and text fields for user interaction. Let's add a button to our frame.

import javax.swing.*;

public class MyFrame {
JFrame frame;
JButton button;

public MyFrame(){
frame = new JFrame();
button = new JButton("Click Me!");

frame.add(button);

frame.setTitle("My First Swing Frame");
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

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

In the above code, we have added a button to our frame using the add() method of the JFrame.

Event Handling in Swing

Swing provides the ability to handle events such as button clicks. Let's modify our code to display a message when the button is clicked.

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

public class MyFrame {
JFrame frame;
JButton button;

public MyFrame(){
frame = new JFrame();
button = new JButton("Click Me!");

button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Button Clicked");
}
});

frame.add(button);

frame.setTitle("My First Swing Frame");
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

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

In the above code, we have added an action listener to our button. When the button is clicked, a message dialog box will appear.

Conclusion

This is a very basic introduction to Java Swing programming. There are many more components and functionalities available in Swing that are not covered in this tutorial. As you learn more about Swing, try to create more complex applications with additional components and functionalities. Happy coding!