简体   繁体   English

单击JButton时如何更新其外观?

[英]How do I update the appearance of my JButton when it's clicked?

I'm working on a Java7 Swing "wizard" type of project that needs to validate a web address before continuing on to the next step of the wizard. 我正在研究Java7 Swing的“向导”类型的项目,该项目需要先验证网址,然后再继续进行向导的下一步。 The validation requires accessing a URL over the internet to verify that expected resources are available. 验证需要通过Internet访问URL来验证期望的资源是否可用。 In some cases, this can take a few seconds which can be long enough to confuse a user. 在某些情况下,这可能需要几秒钟的时间,这可能足以使用户感到困惑。

As a quick solution to this, I would like to disable the "next" button and change the display text while the validation is running so the user knows that the wizard is working and not hung up on anything. 作为对此的快速解决方案,我想在验证运行时禁用“下一步”按钮并更改显示文本,以便用户知道向导正在运行并且不挂任何东西。 The problem is that when I add the code to modify the JButton, none of the changes happen until after the validation has completed. 问题是,当我添加代码以修改JButton时,直到验证完成后才进行任何更改。 This is the case even if I change the button and call revalidate() before I execute the validation methods. 即使执行验证方法之前更改按钮并调用revalidate() ,情况仍是如此。

Here is an excerpt of what I've tried: 这是我尝试过的内容的摘录:

// create next button
next = new JButton("Next", new ImageIcon(getClass().getResource("/navigate_right.png")));

next.setActionCommand("MYACTION");
next.addActionListener(new ActionListener()
{
    @Override
    public void actionPerformed(ActionEvent e)
    {
        System.out.println("Is this the event dispatch thread? "
                + javax.swing.SwingUtilities.isEventDispatchThread());
        System.out.println("Changing button");

        next.setEnabled(false);
        next.setText("Testing Connection");
        next.getRootPane().revalidate();

        System.out.println("Validating Service");
        boolean isValidLocation = ServiceValidator.serviceExists(locationField.getText());

        // ...etc...

When I run this code, the lines "Changing button" and "Validating Service" both get printed before the actual button changes in the display. 当我运行此代码时,“更改按钮”行和“验证服务”行均在实际按钮在显示屏上更改之前被打印出来。 How can I get the JButton to change before System.out.println("Validating Service"); 如何在System.out.println("Validating Service");之前更改JButton System.out.println("Validating Service"); is printed? 被印刷?

The problem is that when I add the code to modify the JButton, none of the changes happen until after the validation has completed. 问题是,当我添加代码以修改JButton时,直到验证完成后才进行任何更改。

Your code is executing on the EDT, so you long running code prevents the GUI from repainting itself until the task is finished executing. 您的代码正在EDT上执行,因此长时间运行的代码会阻止GUI重新绘制自身,直到任务完成执行为止。 You need to use a separate Thread for the long running task, maybe a SwingWorker . 您需要为长期运行的任务使用单独的Thread,也许是SwingWorker Read the section from the Swing tutorial on Concurrency for more information. 阅读Swing 并发教程中的有关更多信息的部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM