How to select all text in a JFormattedTextField when it gets focus?

后端 未结 5 2008
孤独总比滥情好
孤独总比滥情好 2020-12-08 00:53

I have a small Java desktop app that uses Swing. There is a data entry dialog with some input fields of different types (JTextField, JComboBox, JSpinner, JFormattedTextField

5条回答
  •  [愿得一人]
    2020-12-08 01:32

    Wrap your call with SwingUtilities.invokeLater so it will happen after all pending AWT events have been processed :

    pricePerLiter.addFocusListener(new java.awt.event.FocusAdapter() {
        public void focusGained(java.awt.event.FocusEvent evt) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    pricePerLiter.selectAll();
                }
            });
        }
    });
    

提交回复
热议问题