Reputation: 143
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.
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?
Upvotes: 3
Views: 284
Reputation: 285403
Even though I don't fully understand what you're trying to do, I can say without a doubt that you're going about it wrong. Your while (true)
loops shouldn't exist, not in an event-driven system, and belie the concepts of event-driven programming. Instead your code should change states due to reaction to an event, not this never-ending while loop.
Also note that your code as written risks calling a method on simulation before it has been created. Note that it is created in a different thread from where the Run method (which should be named run) is called, and you cannot guarantee order of execution, and in fact likely have wrong order occurring since there will be a delay in calling the Runnable code, since it first has to be queued onto the event thread before being called.
For better help consider creating and posting a minimal, compilable, runnable program that shows for us your problem.
Edit 1
Also, you will want to learn and use Java naming conventions including giving class names that start with upper-case letters and method and variable names that start with lower-case letters.
Edit 2 You state:
Though essentially once this instantiation is done - all the program will do is update itself without any interaction. That was the idea of the loop. The loop will update the simulation, and in turn update what is being displayed.
If you're trying to run a Swing simulation, then why not simply use a Swing Timer for your animation loop? Your code is almost guaranteed to cause a Swing threading mishap.
Edit 3
On 3rd or 4th read, it appears to me that you're trying to display the menu view in a modal fashion from the main application -- that is when menu is active the main application is either not interactable or not visible. If so, consider, 1) using a modal JDialog for your menu window. This way you can launch the window, and the launching code will halt at this location until the menu window is no longer visible. You then can display your animation window. Or 2) swap views using a CardLayout. Here you have a single JFrame application that initially shows your menu JPanel. Then when the menu has been dealt with, you have the CardLayout swap views so that now the animation shows in its place.
Upvotes: 6