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

1

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

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

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

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

    
asked by anonymous 31.10.2014 / 18:07

1 answer

1

Check this code:

(not tested) [1st part]

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

    
31.10.2014 / 18:35