Java by API/javax.swing.text/MaskFormatter — различия между версиями

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

Текущая версия на 14:12, 31 мая 2010

MaskFormatter; setPlaceholderCharacter(char placeholder)

import java.awt.FlowLayout;
import java.text.ParseException;
import javax.swing.BoxLayout;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.text.MaskFormatter;
public class MainClass {
  public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame("Mask Input");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JLabel label;
    JFormattedTextField input;
    JPanel panel;
    MaskFormatter formatter;
    try {
      label = new JLabel("US Phone");
      formatter = new MaskFormatter(""(###")" ###"-####");
      formatter.setPlaceholderCharacter("*");
      input = new JFormattedTextField(formatter);
      input.setColumns(20);
      panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
      panel.add(label);
      panel.add(input);
      frame.add(panel);
    } catch (ParseException e) {
      System.err.println("Unable to add Phone");
    }
    frame.pack();
    frame.setVisible(true);
  }
}





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

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);
  }
}





new MaskFormatter("(###) ###-####") (2)

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);
  }
}





new MaskFormatter("LLLL")

import java.awt.BorderLayout;
import java.text.ParseException;
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("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 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);
  }
}





new MaskFormatter("UUUU")

import java.awt.BorderLayout;
import java.text.ParseException;
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("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 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);
  }
}