I want to display program flow to the user in form of comments in a JTextField. Only the last message (“complete”) is shown. How can I make each message appear when I call setText()?
private class CalculateButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
String keyValue = keywordTF.getText();
currentTF.setText("calling 1");
methodCall1();
currentTF.setText("calling 2");
methodCall2();
currentTF.setText("calling 3");
methodCall3();
currentTF.setText("complete");
}
}
The reason is that the EDT has no time to repaint the text field since your
methodCall*method is running on the EDT.If you want to show progress of a heavy task you should perform the heavy work on a worker thread and update the UI on the EDT. Typically this is achieved by using a
SwingWorkeror usingSwingUtilities#invokeLaterfrom the worker thread.The ‘Concurrency in Swing’ tutorial contains more information