Java Swing GUI Example: Build Interactive Desktop Apps with Code

Robert Jul 13, 2026

In the realm of desktop application development, Java Swing is a popular choice for building GUI (Graphical User Interface) applications. Swing provides a rich set of GUI components that can be easily assembled to create robust and visually appealing applications. Let's dive into Java Swing and explore a practical GUI example to understand its capabilities.

Java swing GUI tutorial #19: BoxLayout
Java swing GUI tutorial #19: BoxLayout

Before we delve into our detailed example, let's briefly touch upon why Java Swing is a viable choice for building GUIs. Firstly, Swing comes with the Java Standard Edition (Java SE), so there's no need for additional libraries or dependencies. Secondly, Swing components are written in Java, enabling developers to create pixel-perfect, customizable GUIs. Lastly, Swing's event-driven programming model allows for responsive and interactive applications.

Basic GUI in Java | Swing GUI Tutorial for Beginners
Basic GUI in Java | Swing GUI Tutorial for Beginners

Java Swing GUI Example: A Simple Notepad Application

To underscore the power and flexibility of Java Swing, let's create a simple notepad application. Our application will have a text area for user input, a toolbar with buttons for basic text formatting, and a menu bar for file operations.

Java Swing Tutorial: How to Create a GUI Application in Java
Java Swing Tutorial: How to Create a GUI Application in Java

To get started, we'll need to import the required Swing components and create the basic frame for our application.

Setting up the Main Frame

Java Swing/GUI Cheat Sheet
Java Swing/GUI Cheat Sheet

The heart of our application is the JFrame, which acts as the main container for our GUI components. We'll set up a simple JFrame with a title, size, and a default close operation.

Here's a sample code snippet to set up the main frame:

```java import javax.swing.*; import java.awt.*; public class SimpleNotepad extends JFrame { public SimpleNotepad() { setTitle("Simple Notepad"); setSize(800, 600); setDefaultCloseOperation(EXIT_ON_CLOSE); } public static void main(String[] args) { SwingUtilities.invokeLater(() -> new SimpleNotepad().setVisible(true)); } } ```

Adding a Text Area for User Input

Java Swings #1  Simple Login UI
Java Swings #1 Simple Login UI

Next, we'll add a JTextArea to our frame for user input. We'll create a JScrollPane to wrap our JTextArea, allowing the text area to scroll if the content exceeds the viewable area.

Here's how you can add a JTextArea to your JFrame:

```java import javax.swing.*; import javax.swing.text.DefaultCaret; import java.awt.*; public class SimpleNotepad extends JFrame { private JTextArea textArea; public SimpleNotepad() { // ... textArea = new JTextArea(10, 40); DefaultCaret caret = (DefaultCaret) textArea.getCaret(); caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE); JScrollPane scrollPane = new JScrollPane(textArea); add(scrollPane, BorderLayout.CENTER); } // ... } ```

Adding a Toolbar for Text Formatting

screenshot of the windows file window with text and numbers highlighted in each section,
screenshot of the windows file window with text and numbers highlighted in each section,

Let's enhance our application by adding a toolbar with buttons for basic text formatting. Swing provides the JToolBar class for creating toolbars, which can be easily populated with JButtons.

Here's a code snippet demonstrating how to add a toolbar with formatting buttons:

Create Swing GUI Project with Design Process in Java
Create Swing GUI Project with Design Process in Java
Java Swing Sign Up using Eclipse WindowBuilder
Java Swing Sign Up using Eclipse WindowBuilder
How to use JList in Java Swing GUI? Tutorial Example
How to use JList in Java Swing GUI? Tutorial Example
How to design a simple dashboard UI using Swing and Java -Netbeans
How to design a simple dashboard UI using Swing and Java -Netbeans
a diagram showing the structure of an object in which it appears to be connected with other objects
a diagram showing the structure of an object in which it appears to be connected with other objects
Top 10 Java Swing Interview Questions Answers asked in Investment banks
Top 10 Java Swing Interview Questions Answers asked in Investment banks
Java Swing Tutorial | How to use JList Using NetBeans (Hindi)
Java Swing Tutorial | How to use JList Using NetBeans (Hindi)
Java Programming Tutorial - 50 - Graphical User Interface GUI
Java Programming Tutorial - 50 - Graphical User Interface GUI
the student registration screen is shown in yellow
the student registration screen is shown in yellow
Nimbus Look and Feel (The Java™ Tutorials >                     Creating a GUI With Swing > Modifying the Look and Feel)
Nimbus Look and Feel (The Java™ Tutorials > Creating a GUI With Swing > Modifying the Look and Feel)
#1 JavaFX UI Design Dashboard Utility
#1 JavaFX UI Design Dashboard Utility
Swing GUI | Display Image on Java Swing | Show Image in JLabel #shorts #javaswing
Swing GUI | Display Image on Java Swing | Show Image in JLabel #shorts #javaswing
Java Calculator App Development Tutorial 1 |  Swing | GUI
Java Calculator App Development Tutorial 1 | Swing | GUI
Difference between Awt and Swing
Difference between Awt and Swing
How to Use HTML in Swing Components (The Java™ Tutorials >                     Creating a GUI With Swing > Using Swing Components)
How to Use HTML in Swing Components (The Java™ Tutorials > Creating a GUI With Swing > Using Swing Components)
Java Swing UI - SignUp Example KControls
Java Swing UI - SignUp Example KControls
JavaFX Vs Java Swing: Which Is Better for Web Application Development?
JavaFX Vs Java Swing: Which Is Better for Web Application Development?
Java Swing Tutorial
Java Swing Tutorial
Using Swing Components: Examples (The Java™ Tutorials >                     Creating a GUI with Swing > Using Swing Components)
Using Swing Components: Examples (The Java™ Tutorials > Creating a GUI with Swing > Using Swing Components)

```java import javax.swing.*; import javax.swing.text.DefaultCaret; import java.awt.*; public class SimpleNotepad extends JFrame { // ... private void createToolbar() { JToolBar toolbar = new JToolBar(); JButton boldButton = new JButton("Bold"); boldButton.addActionListener(e -> textArea.setFont(textArea.getFont().deriveFont(Font.BOLD))); toolbar.add(boldButton); // Add more formatting buttons (italic, underline, etc.) add(toolbar, BorderLayout.NORTH); } public SimpleNotepad() { // ... createToolbar(); } // ... } ```

Adding a Menu Bar for File Operations

Finally, let's add a menu bar for file operations. Swing provides the JMenuBar and JMenu classes for creating menu bars with menus. We'll create a File menu with items for opening, saving, and exiting the application.

Here's an example of how to add a menu bar with a file menu:

```java import javax.swing.*; import javax.swing.text.DefaultCaret; import java.awt.*; import java.io.File; import java.io.FileWriter; import java.io.IOException; public class SimpleNotepad extends JFrame { // ... private void createMenuBar() { JMenuBar menuBar = new JMenuBar(); JMenu fileMenu = new JMenu("File"); JMenuItem openItem = new JMenuItem("Open"); openItem.addActionListener(e -> openFile()); JMenuItem saveItem = new JMenuItem("Save"); saveItem.addActionListener(e -> saveFile()); JMenuItem exitItem = new JMenuItem("Exit"); exitItem.addActionListener(e -> dispose()); fileMenu.add(openItem); fileMenu.add(saveItem); fileMenu.addSeparator(); fileMenu.add(exitItem); menuBar.add(fileMenu); setJMenuBar(menuBar); } private void openFile() { JFileChooser fileChooser = new JFileChooser(); int result = fileChooser.showOpenDialog(null); if (result == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); try (Scanner scanner = new Scanner(file)) { textArea.read(scanner, null); } catch (IOException e) { e.printStackTrace(); } } } private void saveFile() { JFileChooser fileChooser = new JFileChooser(); int result = fileChooser.showSaveDialog(null); if (result == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); try (FileWriter fileWriter = new FileWriter(file)) { textArea.write(new BufferedWriter(fileWriter)); } catch (IOException e) { e.printStackTrace(); } } } public SimpleNotepad() { // ... createMenuBar(); } // ... } ```

Our simple notepad application is now complete. With just a few Swing components, we've created a fully functional notepad with text input, basic text formatting, and file operations. This example showcases the versatility and richness of Java Swing for creating desktop GUI applications.

As you venture further into Java Swing, remember to explore the countless other Swing components and utilities that can enhance your applications. Whether you're creating a simple notepad like this one or a complex desktop application, Java Swing has the tools you need to bring your vision to life. Happy coding!