java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 3947
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to make custom Input Text Formatter in Java E-mail
User Rating: / 11
PoorBest 

Sometimes present formatters are inappropriate to use. In that condition you can define your own custom Input Text Formatter.

This sample code shows the way of defining custom text formatter. It makes a text formatter ‘MyFormatter’ which will allow the integer of range 1 to 1000. If the input in the text field is not in this range then the text box will show invalid.

MyFormatter.java

import java.text.ParseException;
import javax.swing.text.DefaultFormatter;
public class MyFormatter extends DefaultFormatter {
    
    public MyFormatter() {
        super();
    }
    
    // This method reads the text from model class to view class
    public String valueToString(Object arg0throws ParseException {
        return super.valueToString(arg0);
    }
    
    // This method reads the text from view class to modal class and does the validations
    public Object stringToValue(String arg0throws ParseException {
        try {
            int value=Integer.parseInt(arg0);
            if(value>=1&&value<=1000){
                return ""+value;
            }else{
                return "Invalid";
            }
            
        catch (Exception e) {
            return "Invalid";
        }
    }
}

The following class uses the MyFormatter.

MyFormatterExp.java

import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MyFormatterExp extends JFrame{
    
    public MyFormatterExp() {
        
        setTitle("Example: Custom Formatter");
        JPanel panel = new JPanel();
        JLabel label = new JLabel("Number :");
        JFormattedTextField tf=new JFormattedTextField(new MyFormatter());
        tf.setColumns(10);
        panel.add(label);
        panel.add(tf);
        JButton button = new JButton();
        button.setLabel("Click kMe");
        panel.add(button);
        getContentPane().add(panel, BorderLayout.SOUTH);
        pack();
        
    }
    public static void main(String[] args) {
        MyFormatterExp mfe = new MyFormatterExp();
        mfe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mfe.setVisible(true);
    }
}

 Related Tips

 

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.