Java Tutorial/Swing/JFromattedField MaskFormatter
Версия от 17:44, 31 мая 2010; (обсуждение)
Содержание
Apply a mask to String
import javax.swing.text.MaskFormatter;
public class Main {
public static void main(String args[]) throws Exception {
MaskFormatter mf = new MaskFormatter("A-AAAA-AAAA-A");
mf.setValueContainsLiteralCharacters(false);
System.out.println(mf.valueToString("123123123123"));
}
}
//1-2312-3123-1
Formatted Masked Input: new MaskFormatter(###-##-####)
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 MaskInputSample {
public static void main(String args[]) {
JFrame frame = new JFrame("Mask Input");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label;
JFormattedTextField input;
JPanel panel;
MaskFormatter formatter;
BoxLayout layout = new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS);
frame.setLayout(layout);
try {
label = new JLabel("SSN");
formatter = new MaskFormatter("###"-##"-####");
input = new JFormattedTextField(formatter);
input.setValue("123-45-6789");
input.setColumns(20);
panel = new JPanel();
panel.add(label);
panel.add(input);
frame.add(panel);
} catch (ParseException e) {
System.err.println("Unable to add SSN");
}
frame.pack();
frame.setVisible(true);
}
}
Formatted Masked Input: new MaskFormatter((###)###-####) (For phone number)
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 MaskInputSample2 {
public static void main(String args[]) {
JFrame frame = new JFrame("Mask Input");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label;
JFormattedTextField input;
JPanel panel;
MaskFormatter formatter;
BoxLayout layout = new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS);
frame.setLayout(layout);
try {
label = new JLabel("SSN");
formatter = new MaskFormatter(""(###")" ###"-####");
input = new JFormattedTextField(formatter);
input.setValue("(123) 145-6789");
input.setColumns(20);
panel = new JPanel();
panel.add(label);
panel.add(input);
frame.add(panel);
} catch (ParseException e) {
System.err.println("Unable to add SSN");
}
frame.pack();
frame.setVisible(true);
}
}
Input Masks Summary Table
CharacterDescription#Matches numeric character (Character.isDigit())HMatches hexadecimal number (0-9, a-f, and A-F)AMatches alphanumeric character (Character.is LetterOrDigit())?Matches alphabetic character (Character.isLetter())UMatches uppercase letter; maps lowercase to uppercaseLMatches lowercase letter; maps uppercase to lowercase*Wildcard, matches any character"Escape character to have literal strings/separators in input field
Regex Formatter with a JFormattedTextField
/*
*
* Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved.
*
* Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
* modify and redistribute this software in source and binary code form,
* provided that i) this copyright notice and license appear on all copies of
* the software; and ii) Licensee does not utilize the software in a manner
* which is disparaging to Sun.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
* IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
* LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
* LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
* INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
* CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
* OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*
* This software is not designed or intended for use in on-line control of
* aircraft, air traffic, aircraft navigation or aircraft communications; or in
* the design, construction, operation or maintenance of any nuclear
* facility. Licensee represents and warrants that it will not use or
* redistribute the Software for such purposes.
*/
import java.text.ParseException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.text.DefaultFormatter;
/**
* A regular expression based implementation of AbstractFormatter.
*/
class RegexFormatter extends DefaultFormatter {
private Pattern pattern;
private Matcher matcher;
public RegexFormatter() {
super();
}
/**
* Creates a regular expression based AbstractFormatter.
* pattern specifies the regular expression that will be used
* to determine if a value is legal.
*/
public RegexFormatter(String pattern) throws PatternSyntaxException {
this();
setPattern(Pattern.rupile(pattern));
}
/**
* Creates a regular expression based AbstractFormatter.
* pattern specifies the regular expression that will be used
* to determine if a value is legal.
*/
public RegexFormatter(Pattern pattern) {
this();
setPattern(pattern);
}
/**
* Sets the pattern that will be used to determine if a value is legal.
*/
public void setPattern(Pattern pattern) {
this.pattern = pattern;
}
/**
* Returns the Pattern used to determine if a value is legal.
*/
public Pattern getPattern() {
return pattern;
}
/**
* Sets the Matcher used in the most recent test if a value is
* legal.
*/
protected void setMatcher(Matcher matcher) {
this.matcher = matcher;
}
/**
* Returns the Matcher from the most test.
*/
protected Matcher getMatcher() {
return matcher;
}
/**
* Parses text returning an arbitrary Object. Some formatters
* may return null.
*
* If a Pattern has been specified and the text completely
* matches the regular expression this will invoke setMatcher.
*
* @throws ParseException
* if there is an error in the conversion
* @param text
* String to convert
* @return Object representation of text
*/
public Object stringToValue(String text) throws ParseException {
Pattern pattern = getPattern();
if (pattern != null) {
Matcher matcher = pattern.matcher(text);
if (matcher.matches()) {
setMatcher(matcher);
return super.stringToValue(text);
}
throw new ParseException("Pattern did not match", 0);
}
return text;
}
}
public class UsingRegexFormatter {
public static void main(String[] a){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JFormattedTextField formattedField = new JFormattedTextField(new RegexFormatter("\\(\\d{3}\\)\\d{3}-\\d{4}"));
frame.add(formattedField,"North");
frame.add(new JTextField(),"South");
frame.setSize(300, 200);
frame.setVisible(true);
}
}
Work with DefaultFormatterFactory
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.NumberFormat;
import java.text.ParseException;
import javax.swing.BorderFactory;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.NumberFormatter;
/**
* FormatterFactoryDemo.java requires no other files.
*/
public class FormatterFactoryDemo extends JPanel implements
PropertyChangeListener {
// Values for the text fields
private double amount = 100000;
private double rate = .075; // 7.5 %
private int numPeriods = 30;
// Labels to identify the fields
private JLabel amountLabel;
private JLabel rateLabel;
private JLabel numPeriodsLabel;
private JLabel paymentLabel;
// Strings for the labels
private static String amountString = "Loan Amount: ";
private static String rateString = "APR (%): ";
private static String numPeriodsString = "Years: ";
private static String paymentString = "Monthly Payment: ";
// Fields for data entry
private JFormattedTextField amountField;
private JFormattedTextField rateField;
private JFormattedTextField numPeriodsField;
private JFormattedTextField paymentField;
// Formats to format and parse numbers
private NumberFormat amountDisplayFormat;
private NumberFormat amountEditFormat;
private NumberFormat percentDisplayFormat;
private NumberFormat percentEditFormat;
private NumberFormat paymentFormat;
public FormatterFactoryDemo() {
super(new BorderLayout());
setUpFormats();
double payment = computePayment(amount, rate, numPeriods);
// Create the labels.
amountLabel = new JLabel(amountString);
rateLabel = new JLabel(rateString);
numPeriodsLabel = new JLabel(numPeriodsString);
paymentLabel = new JLabel(paymentString);
// Create the text fields and set them up.
amountField = new JFormattedTextField(new DefaultFormatterFactory(
new NumberFormatter(amountDisplayFormat), new NumberFormatter(
amountDisplayFormat), new NumberFormatter(amountEditFormat)));
amountField.setValue(new Double(amount));
amountField.setColumns(10);
amountField.addPropertyChangeListener("value", this);
NumberFormatter percentEditFormatter = new NumberFormatter(
percentEditFormat) {
public String valueToString(Object o) throws ParseException {
Number number = (Number) o;
if (number != null) {
double d = number.doubleValue() * 100.0;
number = new Double(d);
}
return super.valueToString(number);
}
public Object stringToValue(String s) throws ParseException {
Number number = (Number) super.stringToValue(s);
if (number != null) {
double d = number.doubleValue() / 100.0;
number = new Double(d);
}
return number;
}
};
rateField = new JFormattedTextField(new DefaultFormatterFactory(
new NumberFormatter(percentDisplayFormat), new NumberFormatter(
percentDisplayFormat), percentEditFormatter));
rateField.setValue(new Double(rate));
rateField.setColumns(10);
rateField.addPropertyChangeListener("value", this);
numPeriodsField = new JFormattedTextField();
numPeriodsField.setValue(new Integer(numPeriods));
numPeriodsField.setColumns(10);
numPeriodsField.addPropertyChangeListener("value", this);
paymentField = new JFormattedTextField(paymentFormat);
paymentField.setValue(new Double(payment));
paymentField.setColumns(10);
paymentField.setEditable(false);
paymentField.setForeground(Color.red);
// Tell accessibility tools about label/textfield pairs.
amountLabel.setLabelFor(amountField);
rateLabel.setLabelFor(rateField);
numPeriodsLabel.setLabelFor(numPeriodsField);
paymentLabel.setLabelFor(paymentField);
// Lay out the labels in a panel.
JPanel labelPane = new JPanel(new GridLayout(0, 1));
labelPane.add(amountLabel);
labelPane.add(rateLabel);
labelPane.add(numPeriodsLabel);
labelPane.add(paymentLabel);
// Layout the text fields in a panel.
JPanel fieldPane = new JPanel(new GridLayout(0, 1));
fieldPane.add(amountField);
fieldPane.add(rateField);
fieldPane.add(numPeriodsField);
fieldPane.add(paymentField);
// Put the panels in this panel, labels on left,
// text fields on right.
setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
add(labelPane, BorderLayout.CENTER);
add(fieldPane, BorderLayout.LINE_END);
}
/** Called when a field"s "value" property changes. */
public void propertyChange(PropertyChangeEvent e) {
Object source = e.getSource();
if (source == amountField) {
amount = ((Number) amountField.getValue()).doubleValue();
} else if (source == rateField) {
rate = ((Number) rateField.getValue()).doubleValue();
} else if (source == numPeriodsField) {
numPeriods = ((Number) numPeriodsField.getValue()).intValue();
}
double payment = computePayment(amount, rate, numPeriods);
paymentField.setValue(new Double(payment));
}
/**
* Create the GUI and show it. For thread safety, this method should be
* invoked from the event dispatch thread.
*/
private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("FormatterFactoryDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Add contents to the window.
frame.add(new FormatterFactoryDemo());
// Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
// Schedule a job for the event dispatch thread:
// creating and showing this application"s GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// Turn off metal"s use of bold fonts
UIManager.put("swing.boldMetal", Boolean.FALSE);
createAndShowGUI();
}
});
}
// Compute the monthly payment based on the loan amount,
// APR, and length of loan.
double computePayment(double loanAmt, double rate, int numPeriods) {
double I, partial1, denominator, answer;
numPeriods *= 12; // get number of months
if (rate > 0.001) {
I = rate / 12.0; // get monthly rate from annual
partial1 = Math.pow((1 + I), (0.0 - numPeriods));
denominator = (1 - partial1) / I;
} else { // rate ~= 0
denominator = numPeriods;
}
answer = (-1 * loanAmt) / denominator;
return answer;
}
// Create and set up number formats. These objects also
// parse numbers input by user.
private void setUpFormats() {
amountDisplayFormat = NumberFormat.getCurrencyInstance();
amountDisplayFormat.setMinimumFractionDigits(0);
amountEditFormat = NumberFormat.getNumberInstance();
percentDisplayFormat = NumberFormat.getPercentInstance();
percentDisplayFormat.setMinimumFractionDigits(2);
percentEditFormat = NumberFormat.getNumberInstance();
percentEditFormat.setMinimumFractionDigits(2);
paymentFormat = NumberFormat.getCurrencyInstance();
}
}