Hi *
i am creating a Dateformat with "YYYY" a set the Dateformatter into the JFormattedTextField.
I expected a text field where i can only edit 4 digits.
But i can put in even more digits.
Whats wrong ?
|
HTML Code:
|
import java.awt.BorderLayout;
import java.text.SimpleDateFormat;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.text.DateFormatter;
public class JFormattedTF extends JFrame {
public JFormattedTF() {
JPanel panel = new JPanel();
JLabel label = new JLabel("Year :");
SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
DateFormatter df = new DateFormatter(yearFormat);
JFormattedTextField tf = new JFormattedTextField(df);
((DateFormatter)tf.getFormatter()).setAllowsInvalid(false);
tf.setColumns(4);
panel.add(label);
panel.add(tf);
getContentPane().add(panel, BorderLayout.SOUTH);
pack();
}
public static void main(String[] args) {
JFormattedTF tfe = new JFormattedTF();
tfe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tfe.setVisible(true);
}
}
|