January 07, 2012

TextField integer/Number validator in java swing

Example show how to create a text field in java swing that accepts only numbers
initializing variable

private JFormattedTextField expAmount_txt = null;
adding to container

panel.add(getExpAmount_txt(),null);
private JFormattedTextField getExpAmount_txt() {
if (expAmount_txt == null) {
Format general = NumberFormat.getInstance();
expAmount_txt = new JFormattedTextField(general);
expAmount_txt.setBounds(new Rectangle(120, 40, 120, 20));
expAmount_txt.setHorizontalAlignment(JFormattedTextField.RIGHT);
}
return expAmount_txt;
}



Thanking you..........

Facebook comments