1

I have a Text Area swing element where I want to display some execution logs. messages should appear after each execution step. e.g. "Fetching has been started". But the problem is that all logs messages appear only once after the button action performed event is completed.Below is the way I used to display some text

getXmlButton = new JButton("Fetch Reports");
    getXmlButton.addActionListener((new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {

            createDirectory();
            stutusArea.append("Fetching has been started");
            final String password = passwordTextField.getText();
            final String username = loginTextField.getText();

            OperatingSystemDriver os = new OperatingSystemDriver();
            driver = os.getDriver();

            stutusArea.append("getting some url");
            driver.get(URL);



           try {
               LoginPage login = new LoginPage(driver);
               login.loginAs(username, password);
               stutusArea.append("successful login"); 
           } catch (Exception e1) {
               stutusArea.append("login error"); 
            }
    insertToDbButton.setEnabled(true);
        }
    }));
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
noname.404
  • 335
  • 3
  • 11
  • 1
    Don't block the EDT (Event Dispatch Thread) - the GUI will 'freeze' when that happens. Instead implement a `SwingWorker` for long running tasks. See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for more details. – Andrew Thompson Mar 09 '13 at 12:54

2 Answers2

2

You have to use a different thread, otherwise your GUI blocks.

Please note that updating the GUI from an other thread is a bad idea, use SwingUtilities.invokeLater to avoid some strange errors/bugs.

public void actionPerformed(ActionEvent e) {
    new Thread(new Runnable() {
        public void run() {
             SwingUtilities.invokeLater(new Runnable() {
                 public void run() {
                     stutusArea.append("Fetching has been started");
                 }
             })
             final String password = passwordTextField.getText();
             final String username = loginTextField.getText();
             // ...
        }
    }).start();
}
Johannes Kuhn
  • 14,778
  • 4
  • 49
  • 73
1

That's because you do all your ActionPerformed stuff in the GUI thread, so the GUI freezes, and waits for your ActionPerformed stuff to be done, a way to avoid this is using Threads, there is an example: Thread Example

BackSlash
  • 21,927
  • 22
  • 96
  • 136
  • The 2nd linked article shows how to do it in a non-GUI environment but doing it for a GUI takes special considerations. E.G. All GUI updates should be on the EDT, while long running tasks should be moved off it. See my comment above for more details. – Andrew Thompson Mar 09 '13 at 12:57
  • Yes, the second linked article is a generic example, i've read your comment and in fact i didn't know about the SwingWorker, after reading it's JavaDoc i think it's definitely a better way to do it, thanks! – BackSlash Mar 09 '13 at 13:02