Java by API/javax.swing/JFormattedTextField

Материал из Java эксперт
Перейти к: навигация, поиск

JFormattedTextField: addActionListener(ActionListener l)

   <source lang="java">
 

import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.text.DateFormatter; import javax.swing.text.DefaultFormatterFactory; public class MainClass {

 public static void main(String args[]) throws Exception {
   JFrame frame = new JFrame("Formatted Example");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   DateFormat displayFormat = new SimpleDateFormat("yyyy--MMMM--dd");
   DateFormatter displayFormatter = new DateFormatter(displayFormat);
   DateFormat editFormat = new SimpleDateFormat("MM/dd/yy");
   DateFormatter editFormatter = new DateFormatter(editFormat);
   DefaultFormatterFactory factory = new DefaultFormatterFactory(displayFormatter,
       displayFormatter, editFormatter);
   JFormattedTextField date2TextField = new JFormattedTextField(factory, new Date());
   frame.add(date2TextField, BorderLayout.NORTH);
   ActionListener actionListener = new ActionListener() {
     public void actionPerformed(ActionEvent actionEvent) {
       JFormattedTextField source = (JFormattedTextField) actionEvent.getSource();
       Object value = source.getValue();
       System.out.println("Class: " + value.getClass());
       System.out.println("Value: " + value);
     }
   };
   date2TextField.addActionListener(actionListener);
   frame.add(new JTextField(), BorderLayout.SOUTH);
   frame.setSize(250, 100);
   frame.setVisible(true);
 }

}



 </source>
   
  
 
  



JFormattedTextField: commitEdit()

   <source lang="java">
 

import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.ParseException; import javax.swing.JButton; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.border.TitledBorder; import javax.swing.text.DefaultFormatterFactory; import javax.swing.text.MaskFormatter; public class MainClass {

 public static JPanel demo1() {
   JPanel pan = new JPanel(new BorderLayout());
   pan.setBorder(new TitledBorder("change format midstream"));
   MaskFormatter lowercase = null;
   try {
     lowercase = new MaskFormatter("LLLL");
   } catch (ParseException pe) {
   }
   final JFormattedTextField field = new JFormattedTextField(lowercase);
   field.setValue("lower case");
   pan.add(field, BorderLayout.CENTER);
   final JButton change = new JButton("change format");
   JPanel changePanel = new JPanel();
   changePanel.add(change);
   pan.add(changePanel, BorderLayout.SOUTH);
   change.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent ae) {
       try {
         field.rumitEdit();
         MaskFormatter uppercase = new MaskFormatter("UUUU");
         DefaultFormatterFactory factory = new DefaultFormatterFactory(uppercase);
         field.setFormatterFactory(factory);
         change.setEnabled(false);
       } catch (ParseException pe) {
       }
     }
   });
   return pan;
 }
 public static void main(String argv[]) {
   JFrame f = new JFrame("FactoryDemo");
   f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   f.getContentPane().add(demo1(), BorderLayout.NORTH);
   f.getContentPane().add(new JTextField(), BorderLayout.SOUTH);
   f.setSize(240, 160);
   f.setVisible(true);
 }

}


 </source>
   
  
 
  



JFormattedTextField deals with URL

   <source lang="java">
 

import java.awt.GridLayout; import javax.swing.InputVerifier; import javax.swing.JComponent; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class MainClass {

 public static void main(String argv[]) {
   java.net.URL u = null;
   try {
     u = new java.net.URL("http://www.jexp.ru/");
   } catch (java.net.MalformedURLException ignored) {
   }
   JFormattedTextField ftf1 = new JFormattedTextField(u);
   JFormattedTextField ftf2 = new JFormattedTextField(u);
   ftf2.setInputVerifier(new InputVerifier() {
     public boolean verify(JComponent input) {
       if (!(input instanceof JFormattedTextField))
         return true;
       return ((JFormattedTextField) input).isEditValid();
     }
   });
   JPanel p = new JPanel(new GridLayout(0, 2, 3, 8));
   p.add(new JLabel("plain JFormattedTextField:"));
   p.add(ftf1);
   p.add(new JLabel("FTF with InputVerifier:"));
   p.add(ftf2);
   p.add(new JLabel("plain JTextField:"));
   p.add(new JTextField(u.toString()));
   JFrame f = new JFrame("MainClass");
   f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   f.getContentPane().add(new JLabel("Try to delete the colon in each field."), "North");
   f.getContentPane().add(p, "Center");
   f.pack();
   f.setVisible(true);
 }

}


 </source>
   
  
 
  



JFormattedTextField: getFormatterFactory()

   <source lang="java">
 

import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.GregorianCalendar; import javax.swing.JFormattedTextField; import javax.swing.JSpinner; import javax.swing.SpinnerDateModel; import javax.swing.text.DateFormatter; import javax.swing.text.DefaultFormatterFactory; public class Main {

 public static void main(String[] argv) throws Exception {
   Calendar calendar = new GregorianCalendar();
   calendar.set(Calendar.HOUR_OF_DAY, 13); // 1pm
   SpinnerDateModel dateModel = new SpinnerDateModel(calendar.getTime(), null,
       null, Calendar.HOUR_OF_DAY);
   JSpinner spinner = new JSpinner(dateModel);
   JFormattedTextField tf = ((JSpinner.DefaultEditor) spinner.getEditor()).getTextField();
   DefaultFormatterFactory factory = (DefaultFormatterFactory) tf.getFormatterFactory();
   DateFormatter formatter = (DateFormatter) factory.getDefaultFormatter();
   // Change the date format to only show the hours
   formatter.setFormat(new SimpleDateFormat("hh:00 a"));
   //formatter.setFormat(new SimpleDateFormat("HH:00 a"));
 }

}


 </source>
   
  
 
  



JFormattedTextField: getValue()

   <source lang="java">
 

import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.text.DateFormatter; import javax.swing.text.DefaultFormatterFactory; public class MainClass {

 public static void main(String args[]) throws Exception {
   JFrame frame = new JFrame("Formatted Example");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   DateFormat displayFormat = new SimpleDateFormat("yyyy--MMMM--dd");
   DateFormatter displayFormatter = new DateFormatter(displayFormat);
   DateFormat editFormat = new SimpleDateFormat("MM/dd/yy");
   DateFormatter editFormatter = new DateFormatter(editFormat);
   DefaultFormatterFactory factory = new DefaultFormatterFactory(displayFormatter,
       displayFormatter, editFormatter);
   JFormattedTextField date2TextField = new JFormattedTextField(factory, new Date());
   frame.add(date2TextField, BorderLayout.NORTH);
   ActionListener actionListener = new ActionListener() {
     public void actionPerformed(ActionEvent actionEvent) {
       JFormattedTextField source = (JFormattedTextField) actionEvent.getSource();
       Object value = source.getValue();
       System.out.println("Class: " + value.getClass());
       System.out.println("Value: " + value);
     }
   };
   date2TextField.addActionListener(actionListener);
   frame.add(new JTextField(), BorderLayout.SOUTH);
   frame.setSize(250, 100);
   frame.setVisible(true);
 }

}



 </source>
   
  
 
  



JFormattedTextField: isEditValid()

   <source lang="java">
 

import java.awt.GridLayout; import javax.swing.InputVerifier; import javax.swing.JComponent; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class MainClass {

 public static void main(String argv[]) {
   java.net.URL u = null;
   try {
     u = new java.net.URL("http://www.jexp.ru/");
   } catch (java.net.MalformedURLException ignored) {
   }
   JFormattedTextField ftf1 = new JFormattedTextField(u);
   JFormattedTextField ftf2 = new JFormattedTextField(u);
   ftf2.setInputVerifier(new InputVerifier() {
     public boolean verify(JComponent input) {
       if (!(input instanceof JFormattedTextField))
         return true;
       return ((JFormattedTextField) input).isEditValid();
     }
   });
   JPanel p = new JPanel(new GridLayout(0, 2, 3, 8));
   p.add(new JLabel("plain JFormattedTextField:"));
   p.add(ftf1);
   p.add(new JLabel("FTF with InputVerifier:"));
   p.add(ftf2);
   p.add(new JLabel("plain JTextField:"));
   p.add(new JTextField(u.toString()));
   JFrame f = new JFrame("MainClass");
   f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   f.getContentPane().add(new JLabel("Try to delete the colon in each field."), "North");
   f.getContentPane().add(p, "Center");
   f.pack();
   f.setVisible(true);
 }

}


 </source>
   
  
 
  



JFormattedTextField: setColumns(int columns)

   <source lang="java">
 

import java.awt.FlowLayout; import java.awt.Font; import javax.swing.BoxLayout; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField;

public class Main{

 public static void main(String args[]) {
   JFrame frame = new JFrame("Number Input");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   Font font = new Font("SansSerif", Font.BOLD, 16);
   JLabel label;
   JFormattedTextField input;
   JPanel panel;
   BoxLayout layout = new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS);
   frame.setLayout(layout);
   label = new JLabel("Raw Number:");
   input = new JFormattedTextField(2424.50);
   input.setValue(2424.50);
   input.setColumns(20);
   input.setFont(font);
   panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
   panel.add(label);
   panel.add(input);
   frame.add(panel);
   frame.add(new JTextField());
   frame.pack();
   frame.setVisible(true);
 }

}


 </source>
   
  
 
  



JFormattedTextField: setFont(Font f)

   <source lang="java">
 

import java.awt.FlowLayout; import java.awt.Font; import javax.swing.BoxLayout; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField;

public class Main{

 public static void main(String args[]) {
   JFrame frame = new JFrame("Number Input");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   Font font = new Font("SansSerif", Font.BOLD, 16);
   JLabel label;
   JFormattedTextField input;
   JPanel panel;
   BoxLayout layout = new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS);
   frame.setLayout(layout);
   label = new JLabel("Raw Number:");
   input = new JFormattedTextField(2424.50);
   input.setValue(2424.50);
   input.setColumns(20);
   input.setFont(font);
   panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
   panel.add(label);
   panel.add(input);
   frame.add(panel);
   frame.add(new JTextField());
   frame.pack();
   frame.setVisible(true);
 }

}


 </source>
   
  
 
  



JFormattedTextField: setFormatterFactory(AbstractFormatterFactory tf)

   <source lang="java">
 

import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.ParseException; import javax.swing.JButton; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.border.TitledBorder; import javax.swing.text.DefaultFormatterFactory; import javax.swing.text.MaskFormatter; public class MainClass {

 public static JPanel demo1() {
   JPanel pan = new JPanel(new BorderLayout());
   pan.setBorder(new TitledBorder("change format midstream"));
   MaskFormatter lowercase = null;
   try {
     lowercase = new MaskFormatter("LLLL");
   } catch (ParseException pe) {
   }
   final JFormattedTextField field = new JFormattedTextField(lowercase);
   field.setValue("lower case");
   pan.add(field, BorderLayout.CENTER);
   final JButton change = new JButton("change format");
   JPanel changePanel = new JPanel();
   changePanel.add(change);
   pan.add(changePanel, BorderLayout.SOUTH);
   change.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent ae) {
       try {
         field.rumitEdit();
         MaskFormatter uppercase = new MaskFormatter("UUUU");
         DefaultFormatterFactory factory = new DefaultFormatterFactory(uppercase);
         field.setFormatterFactory(factory);
         change.setEnabled(false);
       } catch (ParseException pe) {
       }
     }
   });
   return pan;
 }
 public static void main(String argv[]) {
   JFrame f = new JFrame("FactoryDemo");
   f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   f.getContentPane().add(demo1(), BorderLayout.NORTH);
   f.getContentPane().add(new JTextField(), BorderLayout.SOUTH);
   f.setSize(240, 160);
   f.setVisible(true);
 }

}


 </source>
   
  
 
  



JFormattedTextField: setInputVerifier(InputVerifier inputVerifier)

   <source lang="java">
 

import java.awt.GridLayout; import javax.swing.InputVerifier; import javax.swing.JComponent; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class MainClass {

 public static void main(String argv[]) {
   java.net.URL u = null;
   try {
     u = new java.net.URL("http://www.jexp.ru/");
   } catch (java.net.MalformedURLException ignored) {
   }
   JFormattedTextField ftf1 = new JFormattedTextField(u);
   JFormattedTextField ftf2 = new JFormattedTextField(u);
   ftf2.setInputVerifier(new InputVerifier() {
     public boolean verify(JComponent input) {
       if (!(input instanceof JFormattedTextField))
         return true;
       return ((JFormattedTextField) input).isEditValid();
     }
   });
   JPanel p = new JPanel(new GridLayout(0, 2, 3, 8));
   p.add(new JLabel("plain JFormattedTextField:"));
   p.add(ftf1);
   p.add(new JLabel("FTF with InputVerifier:"));
   p.add(ftf2);
   p.add(new JLabel("plain JTextField:"));
   p.add(new JTextField(u.toString()));
   JFrame f = new JFrame("MainClass");
   f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   f.getContentPane().add(new JLabel("Try to delete the colon in each field."), "North");
   f.getContentPane().add(p, "Center");
   f.pack();
   f.setVisible(true);
 }

}


 </source>
   
  
 
  



JFormattedTextField: setValue(Object value)

   <source lang="java">
 

import java.awt.FlowLayout; import java.awt.Font; import java.text.Format; import java.text.NumberFormat; import java.util.Locale; import javax.swing.BoxLayout; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class MainClass {

 public static void main(String args[]) throws Exception {
   JFrame frame = new JFrame("Number Input");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   Font font = new Font("SansSerif", Font.BOLD, 16);
   JLabel label;
   JFormattedTextField input;
   JPanel panel;
   BoxLayout layout = new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS);
   frame.setLayout(layout);
   Format currency = NumberFormat.getCurrencyInstance(Locale.UK);
   label = new JLabel("UK Currency:");
   input = new JFormattedTextField(currency);
   input.setValue(2424.50);
   input.setColumns(20);
   input.setFont(font);
   panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
   panel.add(label);
   panel.add(input);
   frame.add(panel);
   frame.pack();
   frame.setVisible(true);
 }

}


 </source>
   
  
 
  



new JFormattedTextField(AbstractFormatterFactory factory, Object currentValue)

   <source lang="java">
 

import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.text.DateFormatter; import javax.swing.text.DefaultFormatterFactory; public class MainClass {

 public static void main(String args[]) throws Exception {
   JFrame frame = new JFrame("Formatted Example");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   DateFormat displayFormat = new SimpleDateFormat("yyyy--MMMM--dd");
   DateFormatter displayFormatter = new DateFormatter(displayFormat);
   DateFormat editFormat = new SimpleDateFormat("MM/dd/yy");
   DateFormatter editFormatter = new DateFormatter(editFormat);
   DefaultFormatterFactory factory = new DefaultFormatterFactory(displayFormatter,
       displayFormatter, editFormatter);
   JFormattedTextField date2TextField = new JFormattedTextField(factory, new Date());
   frame.add(date2TextField, BorderLayout.NORTH);
   ActionListener actionListener = new ActionListener() {
     public void actionPerformed(ActionEvent actionEvent) {
       JFormattedTextField source = (JFormattedTextField) actionEvent.getSource();
       Object value = source.getValue();
       System.out.println("Class: " + value.getClass());
       System.out.println("Value: " + value);
     }
   };
   date2TextField.addActionListener(actionListener);
   frame.add(new JTextField(), BorderLayout.SOUTH);
   frame.setSize(250, 100);
   frame.setVisible(true);
 }

}



 </source>
   
  
 
  



new JFormattedTextField(Format format)

   <source lang="java">
 

import java.awt.FlowLayout; import java.awt.Font; import java.text.Format; import java.text.NumberFormat; import java.util.Locale; import javax.swing.BoxLayout; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class MainClass {

 public static void main(String args[]) throws Exception {
   JFrame frame = new JFrame("Number Input");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   Font font = new Font("SansSerif", Font.BOLD, 16);
   JLabel label;
   JFormattedTextField input;
   JPanel panel;
   BoxLayout layout = new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS);
   frame.setLayout(layout);
   Format currency = NumberFormat.getCurrencyInstance(Locale.UK);
   label = new JLabel("UK Currency:");
   input = new JFormattedTextField(currency);
   input.setValue(2424.50);
   input.setColumns(20);
   input.setFont(font);
   panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
   panel.add(label);
   panel.add(input);
   frame.add(panel);
   frame.pack();
   frame.setVisible(true);
 }

}


 </source>
   
  
 
  



new JFormattedTextField(Formatter formatter)

   <source lang="java">
 

import java.awt.BorderLayout; import java.text.ParseException; import javax.swing.Box; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.text.MaskFormatter; public class MainClass {

 public static void main(String args[]) {
   JFrame f = new JFrame("JFormattedTextField Sample");
   f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   Box rowOne = Box.createHorizontalBox();
   rowOne.add(new JLabel("SSN:"));
   try {
     MaskFormatter mf1 = new MaskFormatter("###-##-####");
     rowOne.add(new JFormattedTextField(mf1));
   } catch (ParseException e) {
   }
   f.add(rowOne, BorderLayout.NORTH);
   f.setSize(300, 100);
   f.setVisible(true);
 }

}


 </source>
   
  
 
  



new JFormattedTextField(new DecimalFormat("###,###"))

   <source lang="java">
 

import java.util.Date; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import javax.swing.Box; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.text.MaskFormatter; public class MainClass {

 public static void main(String[] args) throws Exception {
   Box form = Box.createVerticalBox();
   form.add(new JLabel("Name:"));
   form.add(new JTextField("User Name"));
   form.add(new JLabel("Birthday:"));
   JFormattedTextField birthdayField = new JFormattedTextField(new SimpleDateFormat("MM/dd/yy"));
   birthdayField.setValue(new Date());
   form.add(birthdayField);
   form.add(new JLabel("Age:"));
   form.add(new JFormattedTextField(new Integer(32)));
   form.add(new JLabel("Hairs on Body:"));
   JFormattedTextField hairsField = new JFormattedTextField(new DecimalFormat("###,###"));
   hairsField.setValue(new Integer(100000));
   form.add(hairsField);
   form.add(new JLabel("Phone Number:"));
   JFormattedTextField phoneField = new JFormattedTextField(new MaskFormatter("(###)###-####"));
   phoneField.setValue("(314)888-1234");
   form.add(phoneField);
   JFrame frame = new JFrame("User Information");
   frame.getContentPane().add(form);
   frame.pack();
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setVisible(true);
 }

}


 </source>
   
  
 
  



new JFormattedTextField(new MaskFormatter("(###)###-####"))

   <source lang="java">
 

import java.util.Date; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import javax.swing.Box; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.text.MaskFormatter; public class MainClass {

 public static void main(String[] args) throws Exception {
   Box form = Box.createVerticalBox();
   form.add(new JLabel("Name:"));
   form.add(new JTextField("User Name"));
   form.add(new JLabel("Birthday:"));
   JFormattedTextField birthdayField = new JFormattedTextField(new SimpleDateFormat("MM/dd/yy"));
   birthdayField.setValue(new Date());
   form.add(birthdayField);
   form.add(new JLabel("Age:"));
   form.add(new JFormattedTextField(new Integer(32)));
   form.add(new JLabel("Hairs on Body:"));
   JFormattedTextField hairsField = new JFormattedTextField(new DecimalFormat("###,###"));
   hairsField.setValue(new Integer(100000));
   form.add(hairsField);
   form.add(new JLabel("Phone Number:"));
   JFormattedTextField phoneField = new JFormattedTextField(new MaskFormatter("(###)###-####"));
   phoneField.setValue("(314)888-1234");
   form.add(phoneField);
   JFrame frame = new JFrame("User Information");
   frame.getContentPane().add(form);
   frame.pack();
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setVisible(true);
 }

}


 </source>
   
  
 
  



new JFormattedTextField(new SimpleDateFormat("MM/dd/yy"))

   <source lang="java">
 

import java.util.Date; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import javax.swing.Box; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.text.MaskFormatter; public class MainClass {

 public static void main(String[] args) throws Exception {
   Box form = Box.createVerticalBox();
   form.add(new JLabel("Name:"));
   form.add(new JTextField("User Name"));
   form.add(new JLabel("Birthday:"));
   JFormattedTextField birthdayField = new JFormattedTextField(new SimpleDateFormat("MM/dd/yy"));
   birthdayField.setValue(new Date());
   form.add(birthdayField);
   form.add(new JLabel("Age:"));
   form.add(new JFormattedTextField(new Integer(32)));
   form.add(new JLabel("Hairs on Body:"));
   JFormattedTextField hairsField = new JFormattedTextField(new DecimalFormat("###,###"));
   hairsField.setValue(new Integer(100000));
   form.add(hairsField);
   form.add(new JLabel("Phone Number:"));
   JFormattedTextField phoneField = new JFormattedTextField(new MaskFormatter("(###)###-####"));
   phoneField.setValue("(314)888-1234");
   form.add(phoneField);
   JFrame frame = new JFrame("User Information");
   frame.getContentPane().add(form);
   frame.pack();
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setVisible(true);
 }

}


 </source>
   
  
 
  



new JFormattedTextField(Object value)

   <source lang="java">
 

import java.awt.FlowLayout; import java.awt.Font; import javax.swing.BoxLayout; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class MainClass {

 public static void main(String args[]) throws Exception {
   JFrame frame = new JFrame("Number Input");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   Font font = new Font("SansSerif", Font.BOLD, 16);
   JLabel label;
   JFormattedTextField input;
   JPanel panel;
   BoxLayout layout = new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS);
   frame.setLayout(layout);
   label = new JLabel("Raw Number:");
   input = new JFormattedTextField(2424.50);
   input.setValue(2424.50);
   input.setColumns(20);
   input.setFont(font);
   panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
   panel.add(label);
   panel.add(input);
   frame.add(panel);
   frame.pack();
   frame.setVisible(true);
 }

}


 </source>