Make JSpinner Select Text When Focused

前端 未结 2 1415
有刺的猬
有刺的猬 2021-01-12 00:00

I would like to change the behavior of a JSpinner so that when you click on the text, it selects it. This makes it easier to replace the field with the value that you want.

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-12 00:05

    Don't know about a Mac but I've used this code on Windows:

    JSpinner.DefaultEditor editor = (JSpinner.DefaultEditor)spinner.getEditor();
    JTextField textField = editor.getTextField();
    textField.addFocusListener( new FocusAdapter()
    {
        public void focusGained(final FocusEvent e)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                @Override
                public void run()
                {
                    JTextField tf = (JTextField)e.getSource();
                    tf.selectAll();
                }
            });
    
        }
    
    });
    

    I've also used this code for selecting text on a JFormattedTextField.

提交回复
热议问题