Saturday, August 24, 2013

JFormattedTextField

JFormattedTextField is a sub-class of JTextField. It extends the JTextField capability to support formats for values input. For example, you can format a number input as #,###.00 or date input as a short form date.

Constructors of the JFormattedTextField class:
-JFormattedTextField()
creates a formatted text field.
-JFormattedTextField(Format formatter)
creates a formatted text field with the specified formatter.
-JFormattedTextField(Object value)
creates a formatted text field with the specified value. The formatter will be constructed based on this value.

Useful methods of the JFormattedTextField class:
-comitEdit():void
Takes the value from the formatter and sets it as the current value.
-getValue():Object
returns the current valid value.
-isEditValid():boolean
determines whether or not the being edited value is valid.
-setValue(Object value):void
sets the value to the formatted text field.

Example:
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import javax.swing.ImageIcon;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

class JFormattedTextFieldShow extends JFrame implements KeyListener{
JLabel lblnum;
JLabel lbldate;
JFormattedTextField numformatted;
JFormattedTextField dateformatted;
JFormattedTextFieldShow(String title){
setTitle(title);
setSize(600,200);
setLayout(new FlowLayout(FlowLayout.LEFT));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contpane=getContentPane();
NumberFormat numformatter=new DecimalFormat("#,###.00");
numformatted=new JFormattedTextField(numformatter);
numformatted.setColumns(20);
numformatted.addKeyListener(this);
JPanel p=new JPanel();
p.setLayout(new GridLayout(2,3));
p.add(new JLabel("Enter unit price:",JLabel.CENTER));
p.add(numformatted);
lblnum=new JLabel();
p.add(lblnum);

DateFormat dformatter=DateFormat.getDateInstance(DateFormat.SHORT);
dateformatted=new JFormattedTextField(dformatter);
dateformatted.setColumns(20);
dateformatted.addKeyListener(this);
p.add(new JLabel("Enter Sale date:",JLabel.CENTER));
p.add(dateformatted);
lbldate=new JLabel();
p.add(lbldate);
contpane.add(p);
setVisible(true);

}

public void keyPressed(KeyEvent e){

}

public void keyReleased(KeyEvent e){
JFormattedTextField ftxt=(JFormattedTextField)e.getSource();
if(ftxt.equals(numformatted))
checkValid(ftxt,lblnum);
else if(ftxt.equals(dateformatted))
checkValid(ftxt,lbldate);
}
public void keyTyped(KeyEvent e){

}

public void checkValid(JFormattedTextField ftxt, JLabel lbl){
if(ftxt.isEditValid()){
try{
ftxt.commitEdit();
}catch(ParseException e){}
lbl.setIcon(new ImageIcon("d:/valid.png"));

}
else {
lbl.setIcon(new ImageIcon("d:/invalid.png"));

}
}



}

public class JFrameSwing {
public static void main(String[] args){
new JFormattedTextFieldShow("JFormattedTextField");
}
}

JFormattedTextField Swing

No comments:

Post a Comment