问题
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);
}
});
}
}
回答1:
You could use:
timeField.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
来源:https://stackoverflow.com/questions/13649810/swing-maskformatter-enter-numbers-from-right-side-of-the-textfield