A quick demonstration of JFormattedTextField : Formatted TextField « Swing JFC « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Swing JFC » Formatted TextField 




A quick demonstration of JFormattedTextField
A quick demonstration of JFormattedTextField
   
/*
Java Swing, 2nd Edition
By Marc Loy, Robert Eckstein, Dave Wood, James Elliott, Brian Cole
ISBN: 0-596-00408-7
Publisher: O'Reilly 
*/
// SimpleFTF.java
//A quick demonstration of JFormattedTextField.
//

import javax.swing.BoxLayout;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class SimpleFTF extends JPanel {

  public SimpleFTF() {
    JFormattedTextField ftf[] new JFormattedTextField[7];
    String des[] new String[ftf.length]// description of each field

    des[0"Date";
    ftf[0new JFormattedTextField(new java.util.Date());

    des[1"Integer";
    ftf[1new JFormattedTextField(new Integer(90032221));

    des[2"Float";
    ftf[2new JFormattedTextField(new Float(3.14));

    des[3"Float work-around"// manually specify a NumberFormat
    ftf[3new JFormattedTextField(java.text.NumberFormat.getInstance());
    ftf[3].setValue(new Float(3.14));

    des[4"currency";
    ftf[4new JFormattedTextField(java.text.NumberFormat
        .getCurrencyInstance());
    ftf[4].setValue(new Float(5.99));

    des[5"percent";
    ftf[5new JFormattedTextField(java.text.NumberFormat
        .getPercentInstance());
    ftf[5].setValue(new Float(0.33));

    des[6"java.net.URL"// works via 1-arg String constructor and
                 // toString()
    java.net.URL u = null;
    try {
      u = new java.net.URL("http://www.ora.com/");
    catch (java.net.MalformedURLException ignored) {
    }
    ftf[6new JFormattedTextField(u);
    ftf[6].setColumns(24);

    // add each ftf[] to a BoxLayout
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    for (int j = 0; j < ftf.length; j += 1) {
      JPanel borderPanel = new JPanel(new java.awt.BorderLayout());
      borderPanel.setBorder(new javax.swing.border.TitledBorder(des[j]));
      borderPanel.add(ftf[j], java.awt.BorderLayout.CENTER);
      add(borderPanel);
    }
  }

  public static void main(String argv[]) {

    if (argv.length > 0) { // change to command-line locale
      if (argv[0].equalsIgnoreCase("canada"))
        java.util.Locale.setDefault(java.util.Locale.CANADA);
      if (argv[0].equalsIgnoreCase("canada_french"))
        java.util.Locale.setDefault(java.util.Locale.CANADA_FRENCH);
      if (argv[0].equalsIgnoreCase("china"))
        java.util.Locale.setDefault(java.util.Locale.CHINA);
      if (argv[0].equalsIgnoreCase("france"))
        java.util.Locale.setDefault(java.util.Locale.FRANCE);
      if (argv[0].equalsIgnoreCase("germany"))
        java.util.Locale.setDefault(java.util.Locale.GERMANY);
      if (argv[0].equalsIgnoreCase("italy"))
        java.util.Locale.setDefault(java.util.Locale.ITALY);
      if (argv[0].equalsIgnoreCase("japan"))
        java.util.Locale.setDefault(java.util.Locale.JAPAN);
      if (argv[0].equalsIgnoreCase("korea"))
        java.util.Locale.setDefault(java.util.Locale.KOREA);
      if (argv[0].equalsIgnoreCase("prc"))
        java.util.Locale.setDefault(java.util.Locale.PRC);
      if (argv[0].equalsIgnoreCase("taiwan"))
        java.util.Locale.setDefault(java.util.Locale.TAIWAN);
      if (argv[0].equalsIgnoreCase("uk"))
        java.util.Locale.setDefault(java.util.Locale.UK);
      if (argv[0].equalsIgnoreCase("us"))
        java.util.Locale.setDefault(java.util.Locale.US);
    }

    String localeString = java.util.Locale.getDefault().getDisplayName();
    JFrame f = new JFrame("SimpleFTF " + localeString);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setContentPane(new SimpleFTF());
    f.pack();
    f.setVisible(true);
  }
}
           
         
    
    
  














Related examples in the same category
1.different configurations of JFormattedTextField: Numberdifferent configurations of JFormattedTextField: Number
2.Different configurations of JFormattedTextField: DateDifferent configurations of JFormattedTextField: Date
3.JFormattedTextField: an input mask (###) ###-#### for a telephone number
4.Using an InputVerifier with a formatted textfieldUsing an InputVerifier with a formatted textfield
5.A formatter for regular expressions to be used with JFormattedTextFieldA formatter for regular expressions to be used with JFormattedTextField
6.Field with different formats with focus and withoutField with different formats with focus and without
7.Input: any number of hyphen-delimeted numbers. Output: int arrayInput: any number of hyphen-delimeted numbers. Output: int array
8.Formatter Factory DemoFormatter Factory Demo
9.Formatted TextField DemoFormatted TextField Demo
10.Accepting Formatted InputAccepting Formatted Input
11.Formatted TextField ExampleFormatted TextField Example
12.Input Verification Demo Input Verification Demo
13.Creating a Text Field to Display and Edit a Phone Number
14.Creating a Text Field to Display and Edit a social security number
15.Make custom Input Text Formatter in Java
16.Support a date with the custom format: 2009-1-1
17.A BigDecimal object custom formatter
18.A decimal number with one digit following the decimal point;
19.Dynamically change the format
20.Creating a Text Field to Display and Edit a Number
21.Creating a Text Field to Display and Edit a Date
22.Format and validate input field in Java Swing
23.Input Verification Dialog Demo Input Verification Dialog Demo
24.A collection of formatted text fields and a button that displays the field values.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.