RedGrittyBrick

Java

About

Example program

This is from a conversation in newsgroup comp. lang. java. gui in January 2007. A participant was asking how to run one Java program from another. I wanted to illustrate how to put together a GUI app in Java where new windows would be opened as a result of pressing a button without the need to use Runtime.exec() to start a completely new program.

Deployment

When you compile these four .java files you will get four class files. Together they comprise one program. For deployment I would package them into a single executable ClubMaster.jar file.

Flexibility

Note that in the “Controller” section of MainPanel I could, instead of using JOptionPane, create JFrames to hold the new MemberListPanel/EventListPanel or I could add them to a new card in a CardPanel or a JTabbedPane in MainPanel’s “View” section. That is why the main GUI sections are based on JPanels. JPanels can be part of many types of Swing container.

Elaborating MemberListPanel

I’ve omitted any business functionality. I’d maybe have JTables to list the members, I’d have a separate MemberModel that extends AbstractTableModel and fetches the member list data from a database.

For brevity I’ve omitted MemberPanel (singular). I would add ActionListener and add/edit/delete buttons to MemberListPanel, MemberListPanel could have a Controller section like the Controller section of MainPanel to open new windows containing a data entry form defined in MemberPanel

Design patterns

Note my use of the terms Model, View and Controller (MVC). This is an important design pattern in OO languages.

Motivation

When I was starting out learning Swing, An example such as the above might have saved me weeks of work! I hope others find it of some use.

A Java Swing application - ClubMaster

This is a working but minimal example of one way (of many) of organising code for a Swing GUI application.

ClubMaster.java

package ClubApp;
// Licence: GPL RedGrittyBrick.org 2007 :-)

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class ClubMaster {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame f = new JFrame("Club Master");
                f.add(new MainPanel(f));
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.pack();
                f.setVisible(true);
            }
        });
    }
}

MainPanel.java

package ClubApp;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class MainPanel extends JPanel implements ActionListener {

    JFrame parent;

    JButton memberButton, eventButton;

    // View
    MainPanel(JFrame parent) {
        this.parent = parent;
        setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
        memberButton = new JButton("members");
        eventButton = new JButton("events");
        memberButton.addActionListener(this);
        eventButton.addActionListener(this);
        add(memberButton);
        add(eventButton);
    }

    // Controller
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        if (command.equals("members")) {
            JOptionPane.showMessageDialog(parent, 
                    new MemberListPanel(parent));
        } else if (command.equals("events")) {
            JOptionPane.showMessageDialog(parent, 
                    new EventListPanel(parent));
        }
    }
}

MemberListPanel.java

package ClubApp;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class MemberListPanel extends JPanel {
    MemberListPanel(JFrame parent) {
        add(new JLabel("A list of members"));
    }
}
EventListPanel.java
package ClubApp;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class EventListPanel extends JPanel {
    EventListPanel(JFrame parent) {
        add(new JLabel("A list of events"));
    }
}