Java Swing GUI Example Program

Java Swing GUI

Java Swing GUI

This Java source code is an example of a Graphical User Interface (GUI) with different Java Swing GUI elements such as TextBox, Buttons, TextArea, Text Area and Labels. It is implemented using Java Swing API which provides a set of “lightweight” (all-Java language) components. This code example extends the functionality of JFrame class to show a JFrame window and Implements ActionListener on buttons to catch the click event on those buttons.

This Java Swing example uses several Swing components to create a graphical user interface (GUI). Here are the main components used in the code:

  1. JFrame: The main window or frame of the GUI.
  2. JComboBox: A dropdown list or combo box for selecting options.
  3. JLabel: A non-editable text component used to display information or instructions.
  4. JButton: A button component that triggers actions when clicked.
  5. JTextArea: A multi-line text area where you can display or input text.
  6. JScrollPane: A scrollable container that wraps around another component (in this case, the JTextArea) to provide scrolling functionality.
  7. Container: A generic component that can hold and organize other components.
  8. JCheckBox: A checkbox that allows the user to toggle a binary choice, such as enabling or disabling a feature. In this example, it’s labeled “Enable Feature.”
  9. JRadioButton and ButtonGroup: JRadioButton components represent radio buttons that are grouped together using a ButtonGroup. Radio buttons within the same group ensure that only one of them can be selected at a time.
  10. FlowLayout: A layout manager that arranges components in a left-to-right, top-to-bottom flow.
  11. JMenuBar, JMenu, and JMenuItem: JMenuBar represents the menu bar at the top of the frame. JMenu represents a dropdown menu within the menu bar, and JMenuItem represents individual items within the menu.

These components are used to create a simple interactive program with buttons, a combo box, and text areas, providing a basic understanding of building a GUI in Java.

Java Swing GUI Example Code

Here is the java example code to show Swing GUI elements.

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

class Main extends JFrame implements ActionListener
{
    JComboBox help = new JComboBox();

    JLabel txtlabel = new JLabel("Coded by saqib at mycplus.com");

    JButton button1 = new JButton("First button");
    JButton button2 = new JButton("Second button");
    JButton button3 = new JButton("Third button");
    
    JRadioButton radioBtn1 = new JRadioButton("Option 1");
    JRadioButton radioBtn2 = new JRadioButton("Option 2");    
    ButtonGroup radioGroup = new ButtonGroup();
    
    JCheckBox checkBox = new JCheckBox("Enable Feature");

    //text area
    JTextArea output =
            new JTextArea("Output will show here in this GUI box", 14, 45);
    JLabel txtlabel2 = new JLabel("admin@mycplus.com");

    public Main()
    {
        super("Java Swing GUI Example");
        //create window
        setSize(550, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

        //create content container
        Container contentArea = getContentPane();
        contentArea.setBackground(Color.gray);
        //create layout manager
        FlowLayout flowManager = new FlowLayout();
        contentArea.setLayout(flowManager);
        JComboBox help = new JComboBox();
        help.setForeground(Color.black);
        help.setBackground(Color.lightGray);

        setContentPane(contentArea);

        //combo box help
        help.addItem("Help - Dropdown Menu");
        help.addItem("-----------------");
        help.addItem("1. Menu item One");
        help.addItem("2. Menu item Two");
        help.addItem("3. Menu item Three");
        help.addItem("4. Menu item Four");

        contentArea.add(help);

        //text label
        txtlabel.setBackground(Color.darkGray);
        contentArea.add(txtlabel);
        setContentPane(contentArea);

        output.setForeground(Color.green);
        output.setBackground(Color.black);
        contentArea.add(output);
        setContentPane(contentArea);

        //scroll pane
        JScrollPane scroller = new JScrollPane(output,
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        scroller.setForeground(Color.darkGray);
        contentArea.add(scroller);
        setContentPane(contentArea);

        //buttons
        button1.addActionListener(this);
        button1.setBackground(Color.lightGray);
        button1.setForeground(Color.black);
        contentArea.add(button1);
        setContentPane(contentArea);
        button2.addActionListener(this);
        button2.setBackground(Color.lightGray);
        button2.setForeground(Color.black);
        contentArea.add(button2);
        setContentPane(contentArea);
        button3.addActionListener(this);
        button3.setBackground(Color.lightGray);
        button3.setForeground(Color.black);
        contentArea.add(button3);
        setContentPane(contentArea);

        //text label
        txtlabel2.setBackground(Color.darkGray);
        contentArea.add(txtlabel2);
        setContentPane(contentArea);
        
        
        radioGroup.add(radioBtn1);
        radioGroup.add(radioBtn2);
        contentArea.add(radioBtn1);
        setContentPane(contentArea);
        contentArea.add(radioBtn2);
        setContentPane(contentArea);
        
        contentArea.add(checkBox);
        setContentPane(contentArea);
        
        createMenuBar();
        
        
    }

    public void actionPerformed (ActionEvent event)
    {
        if(event.getSource() == button1)
            output.setText("You pressed button1");
        if(event.getSource() == button2)
            output.setText("You pressed button2");
        if(event.getSource() == button3)
            output.setText("You pressed button3");
    }
    
    private void createMenuBar() {
        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        JMenuItem exitMenuItem = new JMenuItem("Exit");

        exitMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        fileMenu.add(exitMenuItem);
        menuBar.add(fileMenu);

        setJMenuBar(menuBar);
    }

    public static void main (String [] args)
    {
        Main eg = new Main();
    }
}

Screenshot of the Swing GUI Example

When you compiler and run the above code, you will see a similar screens of the swing gui example as below.

Java Swing GUI Example

M. Saqib: Saqib is Master-level Senior Software Engineer with over 14 years of experience in designing and developing large-scale software and web applications. He has more than eight years experience of leading software development teams. Saqib provides consultancy to develop software systems and web services for Fortune 500 companies. He has hands-on experience in C/C++ Java, JavaScript, PHP and .NET Technologies. Saqib owns and write contents on mycplus.com since 2004.
Related Post