I understand that JFrames should be safely created on the Event Dispatch Thread (EDT) using invokeLater, and I'm attempting to make two of them within my main method.
Code:
public void run() {
// Handle Menu When Open
menu.setVisible(true);
while(menu.isVisible())
{
if(menu.isShowing() == false) {
showMenu = false;
showSimulation = true;
}
}
menu.setVisible(false);
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
int[] dims = menu.SizeSetting();
simulation = new Simulation(dims[0], dims[1]);
}
});
simulation.run();
}
So, the menu is created (this seems to work fine as the processing required here is very little). After this, the menu is used, a button is clicked at menu.setVisible(false)
is called. Afterwards, I queue a method on the EDT, Simulation is a class derived from JFrame
. It's constructor is as follows:
public Simulation(int width, int height) {
this.setVisible(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Traffic Light System - Simulation");
grid = new Grid(0, 0);
ready = false;
grid = new Grid(width, height);
addComponentsToPane(); // Set up visualization of grid here
// Introduce cars here? e.g. grid.Get(0, 0).addCars(new Car[])?
ready = true;
setVisible(true);
}
Where the addComponentsToPane()
method adds all the neccesary content to the JFrame. The run() method called after this constructor is simply:
public void Run() {
while(isVisible()) {
OneStep();
}
setVisible(false);
}
However, when I run this code, I only see the menu window. When I exit the menu, and when the simulation window should display - the program appears to have closed.
Any ideas what I'm doing wrong here?