Caret position at the end in jformattedtextfield

0

I have a jFormatted textfield with a numeric mask. This means that when you click on the field, the cursor is positioned where the user has clicked, instead of at the beginning, since it is filled with placeholders .
How can I modify this, keeping the numerical restriction?

code:

        MaskFormatter formatter;
        txtNum_1 = new JFormattedTextField();
        try {
            formatter = new MaskFormatter("#########");
            formatter.install(txtNum_1);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }
        txtNum_1.setHorizontalAlignment(JFormattedTextField.LEFT);
        txtNum_1.setCaretPosition(JFormattedTextField.LEFT);  // No funciona
    
asked by eduard ruiz figols 20.07.2017 в 16:54
source

1 answer

0

The solution is to add a focus listener in this way:

MaskFormatter mf = new MaskFormatter ("####")

JFormattedTextField fText = new JFormattedTextField (mf);
fText.setFocusLostBehavior (JFormattedTextField.PERSIST); fText.addFocusListener (new FocusListener () {

        public void focusGained(FocusEvent e) 
        {
            fText.setText(fText.getText().trim());
        }

        public void focusLost(FocusEvent e) {
            // TODO Auto-generated method stub

        }});
    
answered by 21.07.2017 в 10:29