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

后端 未结 5 2017
孤独总比滥情好
孤独总比滥情好 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:10

    In addition to the above, if you want this for all text fields you can just do:

    KeyboardFocusManager.getCurrentKeyboardFocusManager()
        .addPropertyChangeListener("permanentFocusOwner", new PropertyChangeListener()
    {
        public void propertyChange(final PropertyChangeEvent e)
        {
            if (e.getNewValue() instanceof JTextField)
            {
                SwingUtilities.invokeLater(new Runnable()
                {
                    public void run()
                    {
                        JTextField textField = (JTextField)e.getNewValue();
                        textField.selectAll();
                    }
                });
    
            }
        }
    });
    

提交回复
热议问题