简体   繁体   中英

How do I update my JTextField in JOptionPane in real time?

I'm wanting to get rid of the text that is already set in my JTextField when I click inside the JTextField (focus on the JTextField).

Here's my code:

        if (newSerial.isFocusOwner())
        {
            newSerial.setText("");
        }

How do I get this code to run whilst I still have my JOptionPane.showMessageDialog running?

I'm not sure if I understand exactly what you are trying to do, but based on my interpretation of your question, you have a text field that you want to update while a JOptionPage message dialog is displayed.

JOptionPane dialogs block the Swing Event Dispatch Thread (EDT), so you can't update the UI while they are being displayed, unless you do it from a different thread (but that's not great - Swing components should be updated on the EDT). If I read your question correctly, the easiest option might be to replace your JOptionPane.showMessageDialog(...) can with a JDialog that isn't a modal dialog, and therefore, won't block the EDT.

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