Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JFormattedTextField caret position on focus

I am utilizing a few JFormattedTextFields in my program. For some reason when the text field gains focus after a click on the text field, the caret position always jumps to the left (position 0). I would like the caret to end up at the location clicked by the user. So if I click in between two digits, the caret should end up in between those two digits.

So I implemented a FocusListener that would get the click location and set the caret position there.

FocusListener focusListener = new FocusListener(){


    public void focusGained(FocusEvent evt) {

        JFormettedTextField jftf = (JFormattedTextField) evt.getSource();

        //This is where the caret needs to be.
        int dot = jftf.getCaret().getDot(); 

        SwingUtilities.invokeLater( new Runnable() {

        public void run() {
'the textField that has focus'.setCaretPosition('Some how get the evt or dot');              
              }
           });
        }

    public void focusLost (FocusEvent evt) {}

    });

I've tried a number of things to get his to work. I've tried using the final keyword, which works, but only for a single textfield.

I've used set/get methods inside the focus listener to assign the current object, but am not sure about how to make this "safe" (e.g. do they need to be synchronized?).

Maybe there is something I am missing?

like image 820
SharpBarb Avatar asked Feb 04 '10 17:02

SharpBarb


2 Answers

You need to use a MouseListener:

MouseListener ml = new MouseAdapter()
{
    public void mousePressed(final MouseEvent e)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                JTextField tf = (JTextField)e.getSource();
                int offset = tf.viewToModel(e.getPoint());
                tf.setCaretPosition(offset);
            }
        });
    }
};

formattedTextField.addMouseListener(ml);
like image 155
camickr Avatar answered Sep 27 '22 20:09

camickr


This actually happens in AbstractFormatter.install(JFormattedTextField), which is called when the field gains focus.

I'm not sure why it is designed this way, but you can override this behaviour (as long as your formatter does not change the length of the string in the field.)

Example (assuming the field value is an int):

class IntFormatter extends AbstractFormatter {
    @Override
    public void install(final JFormattedTextField ftf) {
        int prevLen = ftf.getDocument().getLength();
        int savedCaretPos = ftf.getCaretPosition();
        super.install(ftf);
        if (ftf.getDocument().getLength() == prevLen) {
            ftf.setCaretPosition(savedCaretPos);
        }
    }

    public Object stringToValue(String text) throws ParseException {
        return Integer.parseInt(text);
    }

    public String valueToString(Object value) throws ParseException {
        return Integer.toString(((Number) value).intValue());
    }
}

Note that this is not the same as the default Integer formatter. The default formatter uses a DecimalFormat that separates groups of digits, e.g. "1,000,000". This makes the task harder as it changes the length of the string.

like image 38
finnw Avatar answered Sep 27 '22 22:09

finnw