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

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

    I know this is kind of old, but I came up with a cleaner solution, without invokeLater:

    private class SelectAllOfFocus extends FocusAdapter {
    
        @Override
        public void focusGained(FocusEvent e) {
            if (! e.isTemporary()) {
                JFormattedTextField textField = (JFormattedTextField)e.getComponent();
                // This is needed to put the text field in edited mode, so that its processFocusEvent doesn't
                // do anything. Otherwise, it calls setValue, and the selection is lost.
                textField.setText(textField.getText());
                textField.selectAll();
            }
        }
    
    }
    
    0 讨论(0)
  • 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();
                    }
                });
    
            }
        }
    });
    
    0 讨论(0)
  • 2020-12-08 01:14

    Thats because the JFormattedTextfield overrides processFocusEvent to format on focus gained/focus lost.

    One sure shot way is to extend JFormattedTextField and override the processFocusEvent method :

    new JFormattedTextField("...") {  
            protected void processFocusEvent(FocusEvent e) {  
                super.processFocusEvent(e);  
                if (e.isTemporary())  
                    return;  
                SwingUtilities.invokeLater(new Runnable() {  
                    @Override  
                    public void run() {  
                        selectAll();  
                    }   
                });  
            }  
        };
    

    Using a focusListener might not always work..since it would depend on the time at which it is called relative to the processFocusEvent.

    0 讨论(0)
  • 2020-12-08 01:31

    The code of camickr can be slightly improved. When the focus passes from a JTextField to another kind of component (such a button), the last automatic selection does not get cleared. It can be fixed this way:

        KeyboardFocusManager.getCurrentKeyboardFocusManager()
            .addPropertyChangeListener("permanentFocusOwner", new PropertyChangeListener()
        {
            @Override
            public void propertyChange(final PropertyChangeEvent e)
            {
    
                if (e.getOldValue() instanceof JTextField)
                {
                        SwingUtilities.invokeLater(new Runnable()
                        {
                                @Override
                                public void run()
                                {
                                        JTextField oldTextField = (JTextField)e.getOldValue();
                                        oldTextField.setSelectionStart(0);
                                        oldTextField.setSelectionEnd(0);
                                }
                        });
    
                }
    
                if (e.getNewValue() instanceof JTextField)
                {
                        SwingUtilities.invokeLater(new Runnable()
                        {
                                @Override
                                public void run()
                                {
                                        JTextField textField = (JTextField)e.getNewValue();
                                        textField.selectAll();
                                }
                        });
    
                }
            }
        });
    
    0 讨论(0)
  • 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();
                }
            });
        }
    });
    
    0 讨论(0)
提交回复
热议问题