简体   繁体   中英

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

你可以使用:

timeField.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM