JTextField不使用Thread.sleep()更新

时间:2013-05-31 15:24:17

标签: java multithreading jtextfield

我试图找出文本字段未更新的原因。我知道使用SwingWorker可能会解决这个问题,但我无法理解为什么它一开始就不起作用。

public class waitExample {

private JFrame frame;
private JTextField txtLeadingText;
private String one = "update string 1";
private String two = "update string 2";
private String three = "update string 3";

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                waitExample window = new waitExample();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public waitExample() {
    initialize();
}

private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    txtLeadingText = new JTextField();
    txtLeadingText.setHorizontalAlignment(SwingConstants.CENTER);
    txtLeadingText.setText("leading text");
    frame.getContentPane().add(txtLeadingText, BorderLayout.SOUTH);
    txtLeadingText.setColumns(10);

    JButton btnClickMeTo = new JButton("CLICK ME TO UPDATE TEXT");
    btnClickMeTo.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent arg0) {

            try {

                updateOne();
                Thread.sleep(1000);
                updateTwo();
                Thread.sleep(1000);
                updateThree();
                Thread.sleep(1000);
                updateLast();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    });
    frame.getContentPane().add(btnClickMeTo, BorderLayout.CENTER);
}

private void updateOne() {

    txtLeadingText.setText(one);
}

private void updateTwo() {

    txtLeadingText.setText(two);
}

private void updateThree() {

    txtLeadingText.setText(three);
}

private void updateLast() {

    txtLeadingText.setText("default text");
    }
}

据我所知,默认的Thread会阻止任何GUI更新。这不重要,因为我在Thread.sleep之前设置了textField。 为什么文本字段不更新?不应该设置文本,然后线程等待吗?

编辑:根据答案,上述代码已更新。

2 个答案:

答案 0 :(得分:3)

您正在EDT上调用Thread.sleep(1000);。这意味着当你的方法结束时 - 只有那时repaint()才会触发(稍后某个时间点)。

在此之前,您的GUI已冻结。

考虑到这是在一个线程上进行的(所以处理很简单):

txtLeadingText.setText(one);
Thread.sleep(1000);
txtLeadingText.setText(two);
Thread.sleep(1000);
txtLeadingText.setText(three);
Thread.sleep(1000);
...
<returning from updateText()>
<processing other events on button click>
...
// some time later
<Swing finds out that GUI needs repaint: calls rapaint()>

这是你应该做的(我没有编译或测试它):

    public class MyRunnable implements Runnable {
        private List<String> strsToSet;
        public MyRunnable(List<String> strsToSet) {
            this.strsToSet = strsToSet;
        }
        @Override
        public void run() {
            try {
                if(strsToSet.size() > 0) {
                    final String str = strsToSet.get(0);
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            txtLeadingText.setText(str);
                        }
                    });

                    Thread.sleep(1000);
                    List<String> newList = new LinkedList<String>(strsToSet);
                    newList.remove(0);
                    new Thread(new MyRunnable(newList)).start();
                }
            }
            catch(InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    new Thread(new MyRunnable(Arrays.asList(one, two, three))).start();

在Swing中很难做到,但在动态语言(如Groovy)中相比,它会变得那么简单(你会更好地掌握正在发生的事情):

    edt { 
        textField.setText(one)
        doOutside { 
            Thread.sleep(1000); 
            edt {
                textField.setText(two)
                doOutside { 
                    Thread.sleep(1000); 
                    edt {
                        textField.setText(three)
                    } 
                }
            } 
        }
    }

答案 1 :(得分:1)

GUI事件循环更新屏幕,但在您返回之前无法更新屏幕。

我建议你不要在GUI事件线程中做任何阻塞操作。