Tuesday, January 01, 2008

How to format and validate input field in Java Swing

This sample code shows the uses of formatted input fields. This code shows a frame with one text filed and a button. The text field is intended to take only integer input and format the number after it lost the focus.


Image

import java.awt.BorderLayout;
import java.text.NumberFormat;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class TextFormatterExp extends JFrame {

public TextFormatterExp() {

setTitle("My First Swing Prog.");
JPanel panel = new JPanel();
JLabel label = new JLabel("Number :");
JFormattedTextField tf = new JFormattedTextField(NumberFormat
.getIntegerInstance());

tf.setColumns(10);
panel.add(label);
panel.add(tf);
JButton button = new JButton();
button.setLabel("Click Me");
panel.add(button);
getContentPane().add(panel, BorderLayout.SOUTH);
pack();
}
public static void main(String[] args) {
TextFormatterExp tfe = new TextFormatterExp();
tfe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tfe.setVisible(true);
}
}

No comments: