![]() |
||
![]() |
![]() Alden Hosting provides professional, efficient, and reliable business-class Web hosting services to small- and medium-sized businesses. |
|
|
JAVA, JSP, SERVLETS, TOMCAT, SERVLETS MANAGER, |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Character | Description |
|---|---|
| # | Any valid number (Character.isDigit). |
| '
(single quote) |
Escape character, used to escape any of the special formatting characters. |
| U | Any character (Character.isLetter). All
lowercase letters are mapped to uppercase. |
| L | Any character (Character.isLetter). All
uppercase letters are mapped to lowercase. |
| A | Any character or number (Character.isLetter
or Character.isDigit). |
| ? | Any character
(Character.isLetter). |
| * | Anything. |
| H | Any hex character (0-9, a-f or A-F). |
When specifying formatters, keep in mind that each formatter object can be used by at most one formatted text field at a time. Each field should have at least one formatter associated with it, of which exactly one is used at any time.You can specify the formatters to be used by a formatted text field in several ways:
- Use the
JFormattedTextFieldconstructor that takes aFormatargument.
A formatter for the field is automatically created that uses the specified format.
- Use the
JFormattedTextFieldconstructor that takes aJFormattedTextField.AbstractFormatterargument.
The specified formatter is used for the field.
- Set the value of a formatted text field that has no format, formatter, or formatter factory specified.
A formatter is assigned to the field by the default formatter factory, using the type of the field's value as a guide. If the value is aDate, the formatter is aDateFormatter. If the value is aNumber, the formatter is aNumberFormatter. Other types result in an instance ofDefaultFormatter.
- Make the formatted text field use a formatter factory that returns customized formatter objects.
This is the most flexible approach. It is useful when you want to associate more than one formatter with a field or add a new kind of formatter to be used for multiple fields. An example of the former use is a field that interprets the user typing in a certain way but displays the value (when the user is not typing) in another way. An example of the latter use is several fields with custom class values, for example,PhoneNumber. You can set up the fields to use a formatter factory that returns specialized formatters for phone numbers.You can set a field's formatter factory either by creating the field using a constructor that takes a formatter factory argument, or by calling the
setFormatterFactorymethod on the field. To create a formatter factory, you can often use an instance ofDefaultFormatterFactoryclass. ADefaultFormatterFactoryobject enables you to specify the formatters returned when a value is being edited, is not being edited, or has a null value.The following figures show an application based on the
FormattedTextFieldDemoexample that uses formatter factories to set multiple editors for the Loan Amount and APR fields. While the user is editing the Loan Amount, the $ character is not used so that the user is not forced to type it. Similarly, while the user is editing the APR field, the % character is not required.Click the Launch button to run FormatterFactoryDemo using Java™ Web Start (download JDK 6). Alternatively, to compile and run the example yourself, consult the example index.
![]()
![]()
The following code that creates the formatters and sets them up by using instances of the
DefaultFormatterFactoryclass:The boldface code highlights the calls toprivate double rate = .075; //7.5 % ... amountField = new JFormattedTextField( new DefaultFormatterFactory( new NumberFormatter(amountDisplayFormat), new NumberFormatter(amountDisplayFormat), new NumberFormatter(amountEditFormat))); ... 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)); ... amountDisplayFormat = NumberFormat.getCurrencyInstance(); amountDisplayFormat.setMinimumFractionDigits(0); amountEditFormat = NumberFormat.getNumberInstance(); percentDisplayFormat = NumberFormat.getPercentInstance(); percentDisplayFormat.setMinimumFractionDigits(2); percentEditFormat = NumberFormat.getNumberInstance(); percentEditFormat.setMinimumFractionDigits(2);DefaultFormatterFactoryconstructors. The first argument to the constructor specifies the default formatter to use for the formatted text field. The second argument specifies the display formatter, which is used when the field does not have the focus. The third argument specifies the edit formatter, which is used when the field has the focus. The code does not use a fourth argument, but if it did, the fourth argument would specify the null formatter, which is used when the field's value is null. Because no null formatter is specified, the default formatter is used when the value is null.The code customizes the formatter that uses
percentEditFormatby creating a subclass of theNumberFormatterclass. This subclass overrides thevalueToStringandstringToValuemethods ofNumberFormatterso that they convert the displayed number to the value actually used in calculations, and convert the value to a number. Specifically, the displayed number is 100 times the actual value. The reason is that the percent format used by the display formatter automatically displays the text as 100 times the value, so the corresponding editor formatter must display the text at the same value. TheFormattedTextFieldDemoexample does not need to take care of this conversion because this demo uses only one format for both display and editing.You can find the code for the entire program in
.FormatterFactoryDemo.java
The following tables list some of the commonly used APIs for using formatted text fields.
Classes Related to Formatted Text Fields Class or Interface Purpose JFormattedTextField Subclass of JTextFieldthat supports formatting arbitrary values.JFormattedTextField.AbstractFormatter The superclass of all formatters for JFormattedTextField. A formatter enforces editing policies and navigation policies, handles string-to-object conversions, and manipulates theJFormattedTextFieldas necessary to enforce the desired policy.JFormattedTextField.AbstractFormatterFactory The superclass of all formatter factories. Each JFormattedTextFielduses a formatter factory to obtain the formatter that best corresponds to the text field's state.DefaultFormatterFactory The formatter factory normally used. Provides formatters based on details such as the passed-in parameters and focus state. DefaultFormatter Subclass of JFormattedTextField.AbstractFormatterthat formats arbitrary objects by using thetoStringmethod.MaskFormatter Subclass of DefaultFormatterthat formats and edits strings using a specified character mask. (For example, seven-digit phone numbers can be specified by using "###-####".)InternationalFormatter Subclass of DefaultFormatterthat uses an instance ofjava.text.Formatto handle conversion to and from aString.NumberFormatter Subclass of InternationalFormatterthat supports number formats by using an instance ofNumberFormat.DateFormatter Subclass of InternationalFormatterthat supports date formats by using an instance ofDateFormat.
JFormattedTextField Methods Method or Constructor Purpose JFormattedTextField()
JFormattedTextField(Object)
JFormattedTextField(Format)
JFormattedTextField(AbstractFormatter)
JFormattedTextField(AbstractFormatterFactory)
JFormattedTextField(AbstractFormatterFactory, Object)Creates a new formatted text field. The Objectargument, if present, specifies the initial value of the field and causes an appropriate formatter factory to be created. TheFormatorAbstractFormatterargument specifies the format or formatter to be used for the field, and causes an appropriate formatter factory to be created. TheAbstractFormatterFactoryargument specifies the formatter factory to be used, which determines which formatters are used for the field.void setValue(Object)
Object getValue()Sets or obtains the value of the formatted text field. You must cast the return type based on how the JFormattedTextFieldhas been configured. If the formatter has not been set yet, callingsetValuesets the formatter to one returned by the field's formatter factory.void setFormatterFactory(AbstractFormatterFactory) Sets the object that determines the formatters used for the formatted text field. The object is often an instance of the DefaultFormatterFactoryclass.AbstractFormatter getFormatter() Obtains the formatter of the formatted text field. The formatter is often an instance of the DefaultFormatterclass.void setFocusLostBehavior(int) Specifies the outcome of a field losing the focus. Possible values are defined in JFormattedTextFieldasCOMMIT_OR_REVERT(the default),COMMIT(commit if valid, otherwise leave everything the same),PERSIST(do nothing), andREVERT(change the text to reflect the value).void commitEdit() Sets the value to the object represented by the field's text, as determined by the field's formatter. If the text is invalid, the value remains the same and a ParseExceptionis thrown.boolean isEditValid() Returns true if the formatter considers the current text to be valid, as determined by the field's formatter.
DefaultFormatter Options Method Purpose void setCommitsOnValidEdit(boolean)
boolean getCommitsOnValidEdit()Sets or obtains values when edits are pushed back to the JFormattedTextField. Iftrue,commitEditis called after every valid edit. This property isfalseby default.void setOverwriteMode(boolean)
boolean getOverwriteMode()Sets or obtains the behavior when inserting characters. If true, new characters overwrite existing characters in the model as they are inserted. The default value of this property istrueinDefaultFormatter(and thus inMaskFormatter) andfalseinInternationalFormatter(and thus inDateFormatterandNumberFormatter).void setAllowsInvalid(boolean)
boolean getAllowsInvalid()Sets or interprets whether the value being edited is allowed to be invalid for a length of time. It is often convenient to enable the user to type invalid values until the commitEditmethod is attempted.DefaultFormatterinitializes this property totrue. Of the standard Swing formatters, onlyMaskFormattersets this property tofalse.
This table lists examples that use formatted text fields and points to where those examples are described.
Example Where Described Notes FormattedTextFieldDemo This section Uses four formatted text fields. SpinnerDemo How to Use Spinners Customizes the appearance of the formatted text fields used by two spinners. SliderDemo3 How to Use Sliders Pairs a formatted text field with a slider to enable an integer value to be edited. Converter Using Models Each ConversionPanelpairs a formatted text field with a slider.TextInputDemo This section Shows how to use text fields, spinners, and formatted text fields together, and demonstrates how to use MaskFormatter. Includes code for selecting the text of the field that has just received the focus.FormatterFactoryDemo This section A variation on FormattedTextFieldDemo that uses formatter factories to specify multiple formatters for two formatted text fields. RegexFormatter Regular Expression Based AbstractFormatter (in The Swing Connection A regular expression formatter that includes source code and information about how it was implemented.
Alden Hosting offers private JVM (Java Virtual Machine), Java Server Pages (JSP), Servlets, and Servlets Manager with our Web Hosting Plans WEB 4 PLAN and WEB 5 PLAN , WEB 6 PLAN .
At Alden Hosting we eat and breathe Java! We are the industry leader in providing affordable, quality and efficient Java web hosting in the shared hosting marketplace. All our sites run on our Java hosing platform configured for optimum performance using Java 1.6, Tomcat 6.0.X, MySQL 5.0.x, Apache 2.2.xx and web application frameworks such as Struts, Hibernate, Cocoon, Ant, etc.
We offer only one type of Java hosting - Private Tomcat. Hosting accounts on the Private Tomcat environment get their very own Tomcat server. You can start and re-start your entire Tomcat server yourself.
![]() |
|
http://alden-servlet-Hosting.com
JSP at alden-servlet-Hosting.com
Servlets at alden-servlet-Hosting.com
Servlet at alden-servlet-Hosting.com
Tomcat at alden-servlet-Hosting.com
MySQL at alden-servlet-Hosting.com
Java at alden-servlet-Hosting.com
sFTP at alden-servlet-Hosting.com
http://alden-tomcat-Hosting.com
JSP at alden-tomcat-Hosting.com
Servlets at alden-tomcat-Hosting.com
Servlet at alden-tomcat-Hosting.com
Tomcat at alden-tomcat-Hosting.com
MySQL at alden-tomcat-Hosting.com
Java at alden-tomcat-Hosting.com
sFTP at alden-tomcat-Hosting.com
http://alden-sftp-Hosting.com
JSP at alden-sftp-Hosting.com
Servlets at alden-sftp-Hosting.com
Servlet at alden-sftp-Hosting.com
Tomcat at alden-sftp-Hosting.com
MySQL at alden-sftp-Hosting.com
Java at alden-sftp-Hosting.com
sFTP at alden-sftp-Hosting.com
http://alden-jsp-Hosting.com
JSP at alden-jsp-Hosting.com
Servlets at alden-jsp-Hosting.com
Servlet at alden-jsp-Hosting.com
Tomcat at alden-jsp-Hosting.com
MySQL at alden-jsp-Hosting.com
Java at alden-jsp-Hosting.com
sFTP at alden-jsp-Hosting.com
http://alden-java-Hosting.com
JSp at alden-java-Hosting.com
Servlets at alden-java-Hosting.com
Servlet at alden-java-Hosting.com
Tomcat at alden-java-Hosting.com
MySQL at alden-java-Hosting.com
Java at alden-java-Hosting.com
sFTP at alden-java-Hosting.com
JSP
Servlets
Tomcat
mysql
Java
JSP
Servlets
Tomcat
mysql
Java
JSP
Servlets
Tomcat
mysql
Java
JSP
Servlets
Tomcat
mysql
Java
JSP at JSP.aldenWEBhosting.com
Servlets at servlets.aldenWEBhosting.com
Tomcat at Tomcat.aldenWEBhosting.com
mysql at mysql.aldenWEBhosting.com
Java at Java.aldenWEBhosting.com
Web Hosts Portal
Web Links
Web Links
Web Hosting
JSP Solutions Web Links
JSP Solutions Web Hosting
Servlets Solutions Web Links
Servlets Solutions Web Hosting
Web Links
Web Links
.
.
.
.
.
.
.
.
.
.
jsp hosting
servlets hosting
web hosting
web sites designed
cheap web hosting
web site hosting
myspace web hosting