文本区域未通过按钮操作执行事件更新 [英] Text Area not getting updated with the button action performed event

查看:140
本文介绍了文本区域未通过按钮操作执行事件更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本区域swing元素,其中我想显示一些执行日志。消息应在每个执行步骤后出现。例如获取已开始。但是问题是所有日志消息只会在按钮操作执行事件完成后出现一次。下面是我用来显示一些文本的方式

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);
        }
    }));


推荐答案

你必须使用不同的线程,否则你的GUI

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

请注意,从其他线程更新GUI是一个坏主意,请使用 SwingUtilities.invokeLater 以避免一些奇怪的错误/缺陷。

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();
}

这篇关于文本区域未通过按钮操作执行事件更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆