I have a program that gets input from the user via swing through a function I’m calling in main. The submit button has an action performed method attached to it. I’m trying to get it to repaint the screen after removing the input files and setting the text to inform the user. It doesn’t do the repaint until after the try/catch with a custom function in it. Not sure what I’m doing wrong I though it would execute in order? Below is my Action preformed attached to my submit button. One note is that if I do a frame.dispose() or setVisibility(false) it will remove the frame, any help would be appreciated. Thanks!!
button.addActionListener(new ActionListener (){
public void actionPerformed(ActionEvent e) {
loc = FileLoc.getText();
name = FileName.getText();
//inform user
area.setText("Attempting To Run Test....");
//backGroundPane contains the user input fields and button
frame.remove(backGroundPane);
frame.repaint();
if(loc != null && name != null &&
!loc.equals("") && !name.equals(""))
{
try {
CallDrivers();
} catch (InterruptedException e1) {
System.out.println("Error Running Function");
//e1.printStackTrace();
}
}
else{
area.setText("There are Blank Fields");
System.out.println("test");
}
}});
You are blocking the EDT (Event dispatching Thread).
The event dispatching thread is reponsible of dispatching all UI-events, one at a time, in the order the have been posted. Events can be amongst:
JComponent.repaint()for example)When you call repaint you are pushing an event on the queue, but as long as the current event (the one of the actionPerformed) is not done, the repaint cannot occur. This is why your repaint only occurs after your try/catch has completed
Read more here: http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html