6

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);
            }
        });
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Steve
  • 71
  • 4
  • Have you tried the [`setComponentOrientation(ComponentOrientation o)`](http://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/JSpinner.html) method? – fireshadow52 Nov 30 '12 at 17:15
  • [`Component.setComponentOrientation(ComponentOrientation)`](http://docs.oracle.com/javase/6/docs/api/java/awt/Component.html#setComponentOrientation%28java.awt.ComponentOrientation%29) FTFY @fireshadow52 :) – Brian Nov 30 '12 at 17:16
  • Thank you Brian and fireshadow52. It works good. But I see one small issue. The caret position is confusing little bit. I expect to see the caret position at the end all the time. Instead it showing at the beginning. Also I observed that the number format after I enter 3 digits 149 (I expect to see that as 1:49 but instead it showing it as 14:9) – Steve Nov 30 '12 at 18:43

1 Answers1

4

You could use:

timeField.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • Thank you Reimeus. It works good. But I see one small issue. The caret position is confusing little bit. I expect to see the caret position at the end all the time. Instead it showing at the beginning. Also I observed that the number format after I enter 3 digits 149 (I expect to see that as 1:49 but instead it showing it as 14:9) – Steve Nov 30 '12 at 18:36
  • This is the default behavior of `JFormattedTextField`. You could have a look at calling [setCaretPosition](http://docs.oracle.com/javase/7/docs/api/javax/swing/text/JTextComponent.html#setCaretPosition%28int%29) from within a new [DocumentListener](http://docs.oracle.com/javase/7/docs/api/javax/swing/event/DocumentListener.html) registered with the component. – Reimeus Nov 30 '12 at 19:18