0

I've added a FocusAdapter to my JFormattedTextField to select its contents when focus is lost, so that the text is selected when tabbed back into but not clicked into.

    final FocusAdapter listener = new FocusAdapter()
    {
        @Override
        public void focusLost(FocusEvent e)
        {
            super.focusLost(e);

            AccessibleEditableText text = e.getComponent().getAccessibleContext().getAccessibleEditableText();

            text.selectText(0, text.toString().length());
        }
    };
    yearPublishedTextField.addFocusListener(listener);

However, this doesn't work for this particular text field since I added the following formatter:

    NumberFormat nf = NumberFormat.getIntegerInstance();
    nf.setGroupingUsed(false);
    yearPublishedTextField.setFormatterFactory(new DefaultFormatterFactory(new NumberFormatter(nf)));

I've noticed that, on reacquiring focus, the caret gets set to the zero position. Can anyone explain why the formatter is doing this, and suggest a way to work around this issue?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 1
    No, you really want to do it the other way round, select the text when focus is gained (or at least that's what I do). When the field loses focus, it's value is verified and updated – MadProgrammer Jul 17 '15 at 09:13
  • @MadProgrammer That was my original solution, but I couldn't figure out how to restrict this to tabbing as opposed to clicking. Any tips? – TrickyTrev Jul 17 '15 at 09:23
  • You only want it select when you tab into the field? – MadProgrammer Jul 17 '15 at 09:35
  • Exactly, where the user is more likely to replace the whole field when tabbing through, but more likely to make partial corrections when using the mouse. But thanks for your input, I may just revert it back to `focusGained` or remove the formatter instead. – TrickyTrev Jul 17 '15 at 09:47
  • You could also use a `MouseListener` to detect a click and test the focused state, but I don't know which would be notified first – MadProgrammer Jul 17 '15 at 10:59
  • @MadProgrammer thats wrong selection selectAll() should be wrapped into invokeLater, there isn't significant difference between JTextField/JFormattedTextField and its derivates inside JComboBox or JSpinner, [as aside OPs code doesn't touched knows bug](http://stackoverflow.com/questions/7378821/jformattedtextfield-issues) – mKorbel Jul 17 '15 at 12:31
  • @mKorbel selectText, selectAll, same thing – MadProgrammer Jul 17 '15 at 21:12

1 Answers1

0

The fact that a formatter was updating the field made my workaround useless, so I solved this by simply formatting the yearPublishedTextField using regex replacement, as I only needed basic formatting anyway.

yearPublishedTextField.addFocusListener(new FocusAdapter()
        {
            @Override
            public void focusLost(FocusEvent e)
            {
                super.focusLost(e);
                yearPublishedTextField.setText(yearPublishedTextField.getText().replaceAll("[^0-9]", ""));
                yearPublishedTextField.setSelectionStart(0);
                yearPublishedTextField.setSelectionEnd(yearPublishedTextField.getText().length());
            }
        });