JFrame is blank until an unrelated while loop in another class finishes...
I am completely stumped on how my computer is doing this. Basically I'm declaring a new frame, like so
Code:
view2.agentWindow(type);
view2.stopActionListener(this);
model2.addAgent(model, 214, 2, 5, "deposit");
Here are the related functions being called:
Code:
public void agentWindow(String name)
{
JPanel panel = new JPanel();
frame = new JFrame(name);
frame.pack();
//set up frame size and location
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension screenSize = tk.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
frame.setSize(screenWidth / 7, screenHeight / 7);
frame.setLocation(screenWidth / 4, screenHeight / 4);
//Creates the text fields
display1 = new JTextField("", 20);
display1.setEditable(false);
panel.add(display1);
display2 = new JTextField("", 20);
display2.setEditable(false);
panel.add(display2);
state = new JTextField("Running...", 20);
state.setEditable(false);
panel.add(state);
// Creates the buttons
stagent = new JButton("Stop Agent");
panel.add(stagent);
dismiss = new JButton("Dismiss");
panel.add(dismiss);
frame.add(panel);
frame.setVisible(true);
}
Code:
public void addAgent(Account model, int aid, long ops, double amount, String type)
{
run = true;
try
{
while (run==true)
{
run = false;
if(type.equals("withdraw"))
{
model.setValue(model.getValue()-amount);
}
else if (type.equals("deposit"))
{
model.setValue(model.getValue()+amount);
}
Thread.sleep(1000 * ops);
}
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
Now what happens is that until the while loop in the model is finished, the new frame just displays as "deposit" with no text fields or buttons. Once the loop is finished, the things appear. It's quite irritating because my loop breaker is supposed to be one of the buttons that aren't appearing.
Any ideas as to why this is happening?
Re: JFrame is blank until an unrelated while loop in another class finishes...
If the loop is running on the GUI's thread, then the GUI can not be updated until you return the thread for it to execute on.
You need to give the GUI code back its thread every time you want something to change and be visible, otherwise nothing is done until it gets control again.
Re: JFrame is blank until an unrelated while loop in another class finishes...