Field with different formats with focus and without : Formatted TextField « Swing JFC « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Swing JFC » Formatted TextFieldScreenshots 
Field with different formats with focus and without
Field with different formats with focus and without

/*
Java Swing, 2nd Edition
By Marc Loy, Robert Eckstein, Dave Wood, James Elliott, Brian Cole
ISBN: 0-596-00408-7
Publisher: O'Reilly 
*/
// FactoryDemo.java
//Demo 1: field with different formats with focus and without.<br>
//Demo 2: Change the format of a field when the user presses a button.
//

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.border.TitledBorder;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;

public class FactoryDemo {

  public static JPanel demo1() {
    // Demo 1: field with different formats with focus and without

    JPanel pan = new JPanel(new BorderLayout());
    pan.setBorder(new TitledBorder("Demo 1: format toggles with focus"));

    MaskFormatter withFocus = null, withoutFocus = null;
    try {
      withFocus = new MaskFormatter("LLLL");
      withoutFocus = new MaskFormatter("UUUU");
    catch (ParseException pe) {
    }

    DefaultFormatterFactory factory = new DefaultFormatterFactory(
        withoutFocus, null, withFocus);

    JFormattedTextField field = new JFormattedTextField(factory);
    field.setValue("Four");
    pan.add(field, BorderLayout.CENTER);

    return pan;
  }

  public static JPanel demo2() {
    // Demo 2: Change the format of a field when the user presses a button.
    // We can't call field.setFormatter() because that's a protected method.
    // (Plus it wouldn't work anyway. The old factory would replace our new
    // formatter with an "old" one next time the field gains or loses
    // focus.)
    // The thing to do is send a new factory to field.setFormatterFactory().

    JPanel pan = new JPanel(new BorderLayout());
    pan.setBorder(new TitledBorder("Demo 2: change format midstream"));

    MaskFormatter lowercase = null;
    try {
      lowercase = new MaskFormatter("LLLL");
    catch (ParseException pe) {
    }
    final JFormattedTextField field = new JFormattedTextField(lowercase);
    field.setValue("Fore");
    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.commitEdit()// commit current edit (if any)
          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(demo2(), BorderLayout.SOUTH);
    f.setSize(240160);
    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. Using an InputVerifier with a formatted textfieldUsing an InputVerifier with a formatted textfield
4. A formatter for regular expressions to be used with JFormattedTextFieldA formatter for regular expressions to be used with JFormattedTextField
5. Input: any number of hyphen-delimeted numbers. Output: int arrayInput: any number of hyphen-delimeted numbers. Output: int array
6. A quick demonstration of JFormattedTextFieldA quick demonstration of JFormattedTextField
7. Formatter Factory DemoFormatter Factory Demo
8. Formatted TextField DemoFormatted TextField Demo
9. Accepting Formatted InputAccepting Formatted Input
10. Formatted TextField ExampleFormatted TextField Example
11. Input Verification Demo Input Verification Demo
12. Input Verification Dialog Demo Input Verification Dialog Demo
w__w_w__._j___a___v__a_2_s___._c___o_m | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.