For each of the different types of windows in my project I created a different class. For example, my main window is an instance of MainWindow. The project settings window is an instance of ProjectSettingsWindow. I also created a class named CustomWindow. I would've named it Window, but that's taken. Gah. This class contains things that all of my windows share, like an initialization method and JPanel. It extends JFrame and all of my other window classes extend CustomWindow.
Sorry this is exceedingly long. But here's the SSCCE: (this is my first question on here, so bear with me)
Main class:
package beat;
public class Main {
public static StartWindow start = new StartWindow();
public static void main(String[] args) {
start.init(300, 100, "choices, choices");
start.display();
}
public static void close() {
//does other things
System.exit(0);
}
}
StartWindow class:
package beat;
import javax.swing.*;
public class StartWindow extends CustomWindow {
public StartWindow() {
eventHandler = new StartWindowEvents(this);
}
JButton newButton = new JButton();
JButton loadButton = new JButton();
//initialize
public void initBranch() {
initButtons();
//other classes have a few groups to initialize, not just one
}
private void initButtons() {
newButton.setText("new project");
newButton.setSize(120,49);
newButton.setLocation(10,10);
newButton.addActionListener(eventHandler);
loadButton.setText("load project");
loadButton.setSize(120,49);
loadButton.setLocation(164,10);
loadButton.addActionListener(eventHandler);
content.add(newButton);
content.add(loadButton);
}
}
StartWindowEvents class:
package beat;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class StartWindowEvents extends CustomWindowEvents {
public StartWindowEvents(CustomWindow w) {
super(w);
}
//if a button is pressed
public void actionPerformed(ActionEvent e) {
if (e.getSource() == Main.start.newButton)
newButton();
else if (e.getSource() == Main.start.loadButton)
loadButton();
}
private void newButton(){
//do the newButton stuff
}
private void loadButton() {
//do the loadButton stuff
}
}
CustomWindow class:
package beat;
import javax.swing.*;
public class CustomWindow extends JFrame {
JPanel content = new JPanel(null);
CustomWindowEvents eventHandler;
public void display() {
//whatever you want to refresh, usually nothing
setVisible(true);
}
public void init(int width, int height, String title) {
pack();
setVisible(false);
setResizable(false);
setLocationRelativeTo(null); //center on screen, but it doesnt work
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
setContentPane(content);
addWindowListener(eventHandler);
setSize(width, height);
setTitle(title);
initBranch();
}
public void initBranch() {
//whatever you want to do after the window is initialized, usually branch to groups of JComponents
}
}
CustomWindowEvents class:
package beat;
import java.awt.event.*;
import javax.swing.JOptionPane;
public class CustomWindowEvents extends WindowAdapter implements ActionListener {
CustomWindow source;
public CustomWindowEvents(CustomWindow w) {
source = w;
}
public void actionPerformed(ActionEvent e) {}
public void windowClosing(WindowEvent e) {
int i = JOptionPane.showConfirmDialog(source,
"DONT DO IT",
"are you sure?",
JOptionPane.YES_NO_OPTION);
if (i == JOptionPane.YES_OPTION)
doClose();
}
public void doClose() {
//whatever you want to do after the window is confirmed closed, usually exit the program
Main.close();
}
}