Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swing - MaskFormatter - Enter Numbers from Right side of the textfield

I'm new to Swing Java development. Can some one help me on this.

I have a jformattedtextfield with maskformatter. it works just fine. But only thing i would like to know is if we can make this to enter the numbers from right. The below code works just fine to enter the numbers from left to right.

Thank you for your time.

Here is the java code i have:

public class MaskFormattedTextExample extends JFrame {

    private static final long serialVersionUID = -1212313123;

    JFormattedTextField timeField;

    public MaskFormattedTextExample() {
        initComponents();
    }

    private void initComponents() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(new Dimension(200, 200));
        getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));

        MaskFormatter mask = null;
        try {
            mask = new MaskFormatter("##:##:##");
            mask.setPlaceholderCharacter('_');
        } catch (ParseException e) {
            e.printStackTrace();
        }

        timeField = new JFormattedTextField(mask);
        timeField.setHorizontalAlignment(JTextField.RIGHT);
        timeField.setCaretPosition(JTextField.RIGHT);

        getContentPane().add(timeField);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                new MaskFormattedTextExample().setVisible(true);
            }
        });
    }
}
like image 967
Steve Avatar asked Oct 05 '22 19:10

Steve


1 Answers

You could use:

timeField.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
like image 99
Reimeus Avatar answered Oct 10 '22 01:10

Reimeus