Using an InputVerifier with a formatted textfield : Formatted TextField « Swing JFC « Java






Using an InputVerifier with a formatted textfield

Using an InputVerifier with a formatted textfield
   
/*
Java Swing, 2nd Edition
By Marc Loy, Robert Eckstein, Dave Wood, James Elliott, Brian Cole
ISBN: 0-596-00408-7
Publisher: O'Reilly 
*/
// FtfInputVerifier.java
//An example of using an InputVerifier with a formatted textfield.
//

import javax.swing.BorderFactory;
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 FtfInputVerifier {

  public static void main(String argv[]) {

    java.net.URL u = null;
    try {
      u = new java.net.URL("http://www.ora.com/");
    } catch (java.net.MalformedURLException ignored) {
    }

    // create two identical JFormattedTextFields
    JFormattedTextField ftf1 = new JFormattedTextField(u);
    JFormattedTextField ftf2 = new JFormattedTextField(u);

    // and set an InputVerifier on one of them
    ftf2.setInputVerifier(new InputVerifier() {
      public boolean verify(JComponent input) {
        if (!(input instanceof JFormattedTextField))
          return true; // give up focus
        return ((JFormattedTextField) input).isEditValid();
      }
    });

    JPanel p = new JPanel(new java.awt.GridLayout(0, 2, 3, 8));
    p.add(new JLabel("plain JFormattedTextField:", JLabel.RIGHT));
    p.add(ftf1);
    p.add(new JLabel("FTF with InputVerifier:", JLabel.RIGHT));
    p.add(ftf2);
    p.add(new JLabel("plain JTextField:", JLabel.RIGHT));
    p.add(new JTextField(u.toString()));
    p.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));

    JFrame f = new JFrame("FtfInputVerifier");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(
        new JLabel("Try to delete the colon in each field.",
            JLabel.CENTER), java.awt.BorderLayout.NORTH);
    f.getContentPane().add(p, java.awt.BorderLayout.CENTER);
    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.A formatter for regular expressions to be used with JFormattedTextFieldA formatter for regular expressions to be used with JFormattedTextField
5.Field with different formats with focus and withoutField with different formats with focus and without
6.Input: any number of hyphen-delimeted numbers. Output: int arrayInput: any number of hyphen-delimeted numbers. Output: int array
7.A quick demonstration of JFormattedTextFieldA quick demonstration of JFormattedTextField
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.