Java Swing, a powerful UI toolkit, enables developers to create desktop applications with rich, interactive graphical components. If you're new to Java Swing or looking to expand your knowledge, you've come to the right place. In this comprehensive guide, we'll explore various Java Swing examples, from simple buttons to complex layouts and custom components.

Before diving into the examples, let's briefly recap what Java Swing offers. Java Swing provides a wide range of pre-built UI components, such as buttons, labels, text fields, and menus. It also supports event handling, making it easy to respond to user interactions. Now, let's get started with our Java Swing examples.

Getting Started with Java Swing
To begin, we need to import the necessary Java Swing libraries and create a JFrame, the main container for Swing applications.

Here's a simple "Hello, World!" example to get you started:
```java import javax.swing.*; import java.awt.*; public class HelloWorld { public static void main(String[] args) { JFrame frame = new JFrame("Hello, World!"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel label = new JLabel("Hello, World!"); frame.add(label); frame.pack(); frame.setVisible(true); } } ```
Swing Components: Buttons and Labels

Java Swing offers a wide variety of components. Let's explore buttons and labels with an example:
```java import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class ButtonExample extends JFrame { private JButton button; private JLabel label; public ButtonExample() { button = new JButton("Click me!"); label = new JLabel("Waiting for click..."); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { label.setText("Button clicked!"); } }); add(button); add(label); pack(); setDefaultCloseOperation(EXIT_ON_CLOSE); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new ButtonExample().setVisible(true); } }); } } ```
Layout Managers: BorderLayout
Java Swing provides several layout managers to organize components within containers. Let's explore BorderLayout:

```java import javax.swing.*; import java.awt.*; public class BorderLayoutExample extends JFrame { public BorderLayoutExample() { setLayout(new BorderLayout()); add(new JButton("North"), BorderLayout.NORTH); add(new JButton("South"), BorderLayout.SOUTH); add(new JButton("East"), BorderLayout.EAST); add(new JButton("West"), BorderLayout.WEST); add(new JLabel("Center"), BorderLayout.CENTER); pack(); setDefaultCloseOperation(EXIT_ON_CLOSE); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new BorderLayoutExample().setVisible(true); } }); } } ```
Advanced Java Swing Examples
Now that we've covered the basics, let's explore some advanced Java Swing examples.
Custom Components

Java Swing allows you to create custom components by extending the JComponent class. Here's a simple example of a custom component that displays a count:
```java import javax.swing.*; import java.awt.*; public class CountComponent extends JComponent { private int count = 0; @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.drawString("Count: " + count, 10, 20); } public void incrementCount() { count++; repaint(); } } ```
JTable for Displaying Data



















Java Swing provides the JTable component for displaying data in a tabular format. Here's an example:
```java import javax.swing.*; import javax.swing.table.DefaultTableModel; import java.awt.*; public class JTableExample extends JFrame { public JTableExample() { String[] columnNames = {"First Name", "Last Name", "Age"}; Object[][] data = { {"John", "Doe", 30}, {"Jane", "Doe", 28}, }; JTable table = new JTable(new DefaultTableModel(data, columnNames)); JScrollPane scrollPane = new JScrollPane(table); add(scrollPane); pack(); setDefaultCloseOperation(EXIT_ON_CLOSE); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new JTableExample().setVisible(true); } }); } } ```
Java Swing offers a wealth of possibilities for creating engaging desktop applications. From simple buttons to complex layouts and custom components, Java Swing provides the tools you need to bring your UI ideas to life. Happy coding!