How to prevent moving the cursor on a jFormattedTextfield with a mask?

Asked

Viewed 101 times

1

I have a field formatted as follows: '### ### ###'

When I click on the field that contains that mask sometimes the cursor gets me in the middle or where I click.

Example: '##|# ### ###'

Is there any way to prevent the cursor from being moved around by clicking on the field?

1 answer

1

Check this code:

(has not been tested) [Part 1]

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);
            }
        });
    }
};

[2nd part]

 MeteONomeDoTeujFormattedTextfield.addMouseListener(ml);

The source link is this

  • I’m still new to these errands, how do I apply this function to my Jformattedtextfield ? is in the on mouseClicked event of my Jformattedtextfield? I have seen thanks ;) I will test

  • Also I gave around, just to try to get it working, try to paste in the constructor the first part, then soon after you call the name of your 'Jformattedtextfield'e paste the second part...

  • Even in the constructor does not work :s

Browser other questions tagged

You are not signed in. Login or sign up in order to post.