简体   繁体   中英

java swing getting a value from a JTextField from a different thread

I know that if I need to change JTextField value from a different thread I need to use SwingUtilities.invokeLater() call. But if I need to get that value from a different thread, can I just use textfield.getText() or I need to use some sort of invokeLater call? Thanks.

如果您对getText()调用的结果可能不确定(即无序)感到满意,那么可以允许使用其他线程来获取值。

Even something as simple as getText() should be called from the event dispatch thread.

If you're in another thread, you can use invokeAndWait :

final String[] textHolder = { null };

EventQueue.invokeAndWait(new Runnable() {
    @Override
    public void run() {
        textHolder[0] = someTextField.getText();
    }
});

String text = textHolder[0];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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