[Home] [Contents] [Index] [Javadoc] | ![]() [Frames] [No Frames] |
[Previous] [Next] |
Example Programs
Examples of Text Fields
Examples of Spin Fields
Examples of Combo Fields
Examples of Popup Fields
This chapter contains example code fragments that demonstrate the common uses of JClass Field components. In most cases, the properties used are exposed in IDEs, making the job of producing a GUI considerably easier. However, even if you are using an IDE, this code will extend a field's capabilities beyond the properties provided in the IDE.
The code listings below are snippets from the examples in the distribution, which contain a main method so that they can be run as an application as well as in a browser.
The table below provides a quick reference to the examples in this chapter.
For Code On... | See... |
---|---|
Using place holder characters to indicate the parts to be filled in; |
JCTextField with String Validator |
Controlling the field's appearance before and during user edit; |
JCTextField with Integer Validator JCSpinField with BigDecimal Validator |
Selecting the contents of the field whenever it receives focus; |
|
Defining a range of valid input, and providing a visual and audio warning to the user when the field is invalid; |
JCTextField with Short Validator |
Defining a range of valid input, and a default value when the user's input is invalid; |
JCTextField with Byte Validator |
Displaying the content of a field as a currency of a given locale; |
JCTextField with Double Validator JCSpinField with Double Validator |
Setting an invalid policy to restore the previous valid value; |
|
Setting an invalid policy to clear the field when the user's input is invalid; |
JCTextField with Float Validator JCSpinField with Integer Validator JCSpinField with Float Validator |
Allowing date input in several formats, and to attempt to complete a partially entered date; |
|
Allowing date input in one format, specified by place holder characters, and converting all characters to uppercase; |
|
JCTextField with Time Validator |
|
JCTextField with IP Address Validator JCSpinField with IP Address Validator |
|
Associating numeric field values in the pick list with text displayed in the spin field; |
|
Allowing the user to enter a value not contained in the pick list; |
JCSpinField with Short Validator |
JCSpinField with DateTime Validator |
|
Allowing the user to pick only a value from the pick list; any other input is cleared; |
|
Presenting the user with a choice of items internally associated with ordinal numbers, for example for database applications; |
The following code snippets are from TextFields.java found in the examples\field directory. Run the examples using the command:
java examples.field.TextFields
This example demonstrates the effect of using place holder characters to supplement the more limited display capabilities of the mask property. You can fill the field with visible underscores to indicate the parts to be filled in.
p.add(new JLabel("String JCTextField: ")); p.add(text1 = new JCTextField()); // create the validator and set its properties JCStringValidator sv = new JCStringValidator(); sv.setMask("(@@@)@@@-@@@@ Ext. @@@"); sv.setPlaceHolderChars("(___)___-____ Ext. ___"); sv.setAllowNull(true); // set the value model and validator text1.setValueModel(new StringValueModel()); text1.setValidator(sv);
![]() |
This example demonstrates how the displayPattern
and editPattern
properties determine the format of the field depending on whether it has focus.
p.add(new JLabel("Integer JCTextField: ")); p.add(text2 = new JCTextField()); // create validator and set its properties JCIntegerValidator iv = new JCIntegerValidator(); iv.setAllowNull(true); iv.setDisplayPattern("0 inches"); iv.setEditPattern(""); // set the value model and validator text2.setValueModel(new IntegerValueModel(new Integer(100000))); text2.setValidator(iv);
![]() |
This field uses a bean property to select the value in the field when it received focus. It also uses the default display and edit formats.
p.add(new JLabel("Long JCTextField: ")); p.add(text3 = new JCTextField()); // create validator and set its properties JCLongValidator lv = new JCLongValidator(); lv.setAllowNull(true); // set the value model and validator text3.setValueModel(new LongValueModel(new Long(1000000000000l))); text3.setValidator(lv); text3.setSelectOnEnter(true);
![]() |
This example illustrates the use of a validator to confine user input to an acceptable range, and to provide a visual warning to the user when the field is invalid.
p.add(new JLabel("Short JCTextField: ")); p.add(text4 = new JCTextField()); // create the validator and set its properties JCShortValidator shv = new JCShortValidator(); shv.setAllowNull(true); shv.setRange(new Short((short)0), new Short((short)10)); // set the invalid info properties JCInvalidInfo shii = text4.getInvalidInfo(); shii.setInvalidBackground(Color.red); // set value model, validator, and invalidinfo text4.setValueModel(new ShortValueModel(new Short((short)10))); text4.setValidator(shv); text4.setInvalidInfo(shii);
![]() |
This example shows how the invalidPolicy
, JCInvalidInfo.RESTORE_DEFAULT
forces the field to display the default value after the user attempts to commit a number to the field that is out of range.
p.add(new JLabel("Byte JCTextField: ")); p.add(text5 = new JCTextField()); // create the validator and set its properties JCByteValidator bytev = new JCByteValidator(); bytev.setDefaultValue(new Byte((byte)5)); bytev.setAllowNull(true); bytev.setRange(new Byte((byte)1), new Byte((byte)10)); // set the invalidinfo properties JCInvalidInfo byteii = text5.getInvalidInfo(); byteii.setInvalidPolicy(JCInvalidInfo.RESTORE_DEFAULT); // set the value model, validator, and invalidinfo text5.setValueModel(new ByteValueModel(new Byte((byte)1))); text5.setValidator(bytev); text5.setInvalidInfo(byteii);
![]() |
The double validator associated with this text field is augmented by the isCurrency
property so that the value is treated as currency. The display format uses the currency format associated with the current locale.
p.add(new JLabel("Double JCTextField (currency): ")); p.add(text6 = new JCTextField()); // create validator and set its properties JCDoubleValidator dv = new JCDoubleValidator(); dv.setAllowNull(true); dv.setCurrency(true); // set value and validator text6.setValueModel(new DoubleValueModel(new Double(100.00))); text6.setValidator(dv);
![]() |
This field does not allow null values, so when the field is cleared, the invalidPolicy
forces the field to display the previous valid value.
p.add(new JLabel("BigDecimal JCTextField: ")); p.add(text7 = new JCTextField()); // create validator and set its properties JCBigDecimalValidator bdv = new JCBigDecimalValidator(); bdv.setAllowNull(false); // set the invalidinfo properties JCInvalidInfo bdii = text7.getInvalidInfo(); bdii.setInvalidPolicy(JCInvalidInfo.RESTORE_PREVIOUS); // set the value model, validator, and invalidinfo text7.setValueModel(new BigDecimalValueModel(new BigDecimal(100000.11111))); text7.setValidator(bdv); text7.setInvalidInfo(bdii);
![]() |
The invalidPolicy
for this field forces it to clear when the user enters an invalid value.
p.add(new JLabel("Float JCTextField: ")); p.add(text8 = new JCTextField()); // create the validator and set its properties JCFloatValidator fv = new JCFloatValidator(); fv.setRange(new Float((float)-10000), new Float((float)10000)); fv.setAllowNull(true); // set the invalidinfo properties JCInvalidInfo fii = text8.getInvalidInfo(); fii.setInvalidPolicy(JCInvalidInfo.CLEAR_FIELD); // set the value model, validator, and invalidinfo text8.setValidator(fv); text8.setValueModel(new FloatValueModel(new Float(100.111))); text8.setInvalidInfo(fii);
![]() |
This example allows the user to enter date values in several formats. Because the maskInput
property is set to false
, when the user enters a partial date that meets one of the allowed formats, the field attempts to complete the date.
p.add(new JLabel("DateTime(Calendar) JCTextField: ")); p.add(text9 = new JCTextField()); // create validator and set its properties JCDateTimeValidator dtv = new JCDateTimeValidator(); dtv.setMaskInput(false); dtv.setEditFormats(new String[] {"yyyy/MM/dd", "MMM d, yyyy"}); dtv.setAllowNull(true); // set value model and validator text9.setValueModel(new CalendarValueModel()); text9.setValidator(dtv);
![]() |
The format
property works as a mask for Date/Time validators. If you set maskInput
to true
, this field will only allow input that is in the format specified by the format
property. It also prompts the user with place holder characters and uses the casePolicy
property to convert all characters to uppercase.
p.add(new JLabel("Date JCTextField: ")); p.add(text10 = new JCTextField()); // create the validator and set its properties JCDateValidator datev = new JCDateValidator(); datev.setFormat("MMM dd/yy"); datev.setMaskInput(true); datev.setPlaceHolderChars("MMM DD/YY"); datev.setCasePolicy(JCDateValidator.UPPERCASE); datev.setAllowNull(true); // set value model and validator text10.setValueModel(new DateValueModel()); text10.setValidator(datev);
![]() |
You use this field and validator combination to display and update time information. You can maintain a running clock if you wish. One way is to start a thread that sleeps for one second, then fires an event. You catch the event and update the time field using setValue().
This example shows the defaultDetail
's FULL
setting.
p.add(new JLabel("Time JCTextField: ")); p.add(text11 = new JCTextField()); // create the validator and set its properties JCTimeValidator timev = new JCTimeValidator(); timev.setMaskInput(true); timev.setDefaultDetail(JCTimeValidator.FULL); timev.setAllowNull(false); // set the invalidinfo properties JCInvalidInfo timeii = text11.getInvalidInfo(); timeii.setInvalidPolicy(JCInvalidInfo.RESTORE_DEFAULT); // set value model, validator, and invalidinfo text11.setValueModel(new DateValueModel()); text11.setValidator(timev); text11.setInvalidInfo(timeii);
![]() |
You use this field and validator combination to display IP addresses. The setIPValidators()
method takes an array of JCIntegerValidators
and uses their min and max values.
p.add(new JLabel("JCIPAddress JCTextField: ")); p.add(text12 = new JCTextField()); // create the validator and set its properties JCIPAddressValidator ipv = new JCIPAddressValidator(); JCIntegerValidator[] validators = new JCIntegerValidator[4]; validators[0] = new JCIntegerValidator(); validators[0].setMin(new Integer(1)); validators[0].setMax(new Integer(128)); validators[1] = new JCIntegerValidator(); validators[1].setMin(new Integer(30)); validators[1].setMax(new Integer(50)); validators[2] = new JCIntegerValidator(); validators[2].setMin(new Integer(1)); validators[2].setMax(new Integer(10)); validators[3] = new JCIntegerValidator(); validators[3].setMin(new Integer(100)); validators[3].setMax(new Integer(200)); ipv.setIPValidators(validators); // set value model and validator text12.setValueModel(new IPAddressValueModel()); text12.setValidator(ipv); text12.setValue(new JCIPAddress("121.35.2.150"));
![]() |
The following code snippets are from SpinFields.java found in the examples\field directory. Run the examples with the command:
java examples.field.SpinFields
This example uses the mask
property and place holder characters to provide clues about the kind of input the field is expecting.
String validators use JCValidator.SPIN_WRAP
as the default spin policy.
p.add(new JLabel("String JCSpinField: ")); p.add(spin1 = new JCSpinField()); // create the validator and set its properties JCStringValidator sv = new JCStringValidator(); String[] string_values = {"4165941026620", "8005551234567", "5195555941323"}; sv.setMask("(@@@)@@@-@@@@ Ext. @@@"); sv.setPlaceHolderChars("(___)___-____ Ext. ___"); sv.setAllowNull(true); sv.setPickList(new JCListModel(string_values)); // set the value model and validator spin1.setValueModel(new StringValueModel()); spin1.setValidator(sv);
![]() |
There is no display list associated with the pick list in this example. The pick list values themselves appear in the field. Since matchPickList
is true
by default, only four values are possible, 1, 2, 3, and 4. Any attempt by the user to type other values in the field will result in it being cleared.
p.add(new JLabel("Integer JCSpinField: ")); p.add(spin2 = new JCSpinField()); // create validator and set its properties JCIntegerValidator iv = new JCIntegerValidator(); Integer[] int_values = {new Integer(1), new Integer(2), new Integer(3), new Integer(4)}; iv.setAllowNull(true); iv.setPickList(new JCListModel(int_values)); iv.setSpinPolicy(JCIntegerValidator.SPIN_WRAP); // create the invalidinfo and set its properties JCInvalidInfo iii = spin2.getInvalidInfo(); iii.setInvalidPolicy(JCInvalidInfo.CLEAR_FIELD); // set value model, validator, and invalidinfo spin2.setValueModel(new IntegerValueModel()); spin2.setValidator(iv); spin2.setInvalidInfo(iii);
![]() |
This field uses the displayList
property to associate the numeric field values in the pick list with text that will be displayed in the field. Notice that by default the top spin arrow is disabled when the last title in the array is reached.
p.add(new JLabel("Long JCSpinField: ")); p.add(spin3 = new JCSpinField()); // create validator and set its properties JCLongValidator lv = new JCLongValidator(); Long[] long_values = {new Long(1), new Long(2), new Long(3), new Long(4)}; String[] long_display = {"Mr.", "Mrs.", "Ms.", "Miss", "Dr."}; lv.setMatchPickList(true); lv.setAllowNull(true); lv.setPickList(new JCListModel(long_values)); lv.setDisplayList(long_display); // set the value model and validator spin3.setValueModel(new LongValueModel()); spin3.setValidator(lv);
![]() |
In this example, the matchPickList property is set to false
, so that the user is able to enter a value not contained in the pick list.
p.add(new JLabel("Short JCSpinField: ")); p.add(spin4 = new JCSpinField()); // create the validator and set its properties JCShortValidator shv = new JCShortValidator(); Short[] short_values = {new Short((short)1), new Short((short)2), new Short((short)3), new Short((short)4)}; shv.setMatchPickList(false); shv.setAllowNull(true); shv.setPickList(new JCListModel(short_values)); // create the invalidinfo and set its properties JCInvalidInfo shii = spin4.getInvalidInfo(); shii.setInvalidPolicy(JCInvalidInfo.RESTORE_DEFAULT); // set value model, validator, and invalidinfo spin4.setValueModel(new ShortValueModel()); spin4.setValidator(shv); spin4.setInvalidInfo(shii);
![]() |
Here, we set limits on the field using the setRange()
method. Alternatively you can set limits in your IDE. The equivalent statements are:
bytev.setMin(0); bytev.setMax(10);
The setRange()
method makes the program slightly easier to maintain because the numerical limits are kept together in one statement.
The field also sets an invalid policy which turns the background red when the user enters a value that is out of range.
p.add(new JLabel("Byte JCSpinField: ")); p.add(spin5 = new JCSpinField()); // create the validator and set its properties JCByteValidator bytev = new JCByteValidator(); bytev.setAllowNull(true); bytev.setRange(new Byte((byte)0), new Byte((byte)10)); // create the invalidinfo and set its properties JCInvalidInfo byteii = spin5.getInvalidInfo(); byteii.setInvalidBackground(Color.red); // set the value model, validator, and invalidinfo spin5.setValueModel(new ByteValueModel()); spin5.setValidator(bytev); spin5.setInvalidInfo(byteii);
![]() |
The isCurrency
property in this field is set to true
so the value will be treated as currency. The field also uses an increment value of five.
p.add(new JLabel("Double JCSpinField (currency): ")); p.add(spin6 = new JCSpinField()); // create validator and set its properties JCDoubleValidator dv = new JCDoubleValidator(); dv.setAllowNull(true); dv.setCurrency(true); dv.setIncrement(new Double(5.0)); // set value and validator spin6.setValueModel(new DoubleValueModel()); spin6.setValidator(dv);
![]() |
Setting the display pattern, as in this field, gives the user the context for the value entered.
p.add(new JLabel("BigDecimal JCSpinField: ")); p.add(spin7 = new JCSpinField()); // create validator and set its properties JCBigDecimalValidator bdv = new JCBigDecimalValidator(); bdv.setAllowNull(false); bdv.setDisplayPattern("0.00 inches"); bdv.setEditPattern(""); // create the invalidinfo and set its properties JCInvalidInfo bdii = spin7.getInvalidInfo(); bdii.setInvalidPolicy(JCInvalidInfo.RESTORE_DEFAULT); // set the value model, validator, and invalidinfo spin7.setValueModel(new BigDecimalValueModel()); spin7.setValidator(bdv); spin7.setInvalidInfo(bdii);
![]() |
This example sets the increment value to 0.1. The invalid policy will clear the field if the user enters an invalid value.
p.add(new JLabel("Float JCSpinField: ")); p.add(spin8 = new JCSpinField()); // create the validator and set its properties JCFloatValidator fv = new JCFloatValidator(); fv.setIncrement(new Float(0.1)); fv.setAllowNull(true); // create the invalidinfo and set its properties JCInvalidInfo fii = spin8.getInvalidInfo(); fii.setInvalidPolicy(JCInvalidInfo.CLEAR_FIELD); // set the value model, validator, and invalidinfo spin8.setValidator(fv); spin8.setValueModel(new FloatValueModel()); spin8.setInvalidInfo(fii);
![]() |
The default spin policy for date and time validators is JCValidator.SPIN_SUBFIELD
, which allows the user to click a single set of arrow buttons to manipulate the sub-fields that comprise a complete date and time specification.
p.add(new JLabel("DateTime(Calendar) JCSpinField: ")); p.add(spin9 = new JCSpinField()); // create validator and set its properties JCDateTimeValidator dtv = new JCDateTimeValidator(); dtv.setMaskInput(true); dtv.setAllowNull(true); // set value model and validator spin9.setValueModel(new CalendarValueModel()); spin9.setValidator(dtv);
![]() |
The format
property for date and time validators is useful for presenting the value of the field in a way that is familiar to a specific group of users.
p.add(new JLabel("Date JCSpinField: ")); p.add(spin10 = new JCSpinField()); // create the validator and set its properties JCDateValidator datev = new JCDateValidator(); datev.setMaskInput(true); datev.setFormat("MMMM d 'yy"); // set value model and validator spin10.setValueModel(new DateValueModel()); spin10.setValidator(datev);
![]() |
A basic spin field with the time validator takes the current time as its default. This field presents the default time in full format.
p.add(new JLabel("Time JCSpinField: ")); p.add(spin11 = new JCSpinField()); // create the validator and set its properties JCTimeValidator timev = new JCTimeValidator(); timev.setDefaultDetail(JCTimeValidator.FULL); // create the invalidinfo and set its properties JCInvalidInfo timeii = spin11.getInvalidInfo(); timeii.setInvalidPolicy(JCInvalidInfo.RESTORE_DEFAULT); // set value model, validator, and invalidinfo spin11.setValueModel(new DateValueModel()); spin11.setValidator(timev); spin11.setInvalidInfo(timeii);
![]() |
You use this field and validator combination to display IP addresses. The setIPValidators()
method takes an array of JCIntegerValidators
and uses their min and max values.
p.add(new JLabel("JCIPAddress JCSpinField: ")); p.add(spin12 = new JCSpinField()); // create the validator and set its properties JCIPAddressValidator ipv = new JCIPAddressValidator(); JCIntegerValidator[] validators = new JCIntegerValidator[4]; validators[0] = new JCIntegerValidator(); validators[0].setMin(new Integer(1)); validators[0].setMax(new Integer(128)); validators[1] = new JCIntegerValidator(); validators[1].setMin(new Integer(30)); validators[1].setMax(new Integer(50)); validators[2] = new JCIntegerValidator(); validators[2].setMin(new Integer(1)); validators[2].setMax(new Integer(10)); validators[3] = new JCIntegerValidator(); validators[3].setMin(new Integer(100)); validators[3].setMax(new Integer(200)); ipv.setIPValidators(validators); // set value model and validator spin12.setValueModel(new IPAddressValueModel()); spin12.setValidator(ipv); spin12.setValue(new JCIPAddress("121.35.2.150"));
![]() |
The following code snippets are from ComboFields.java found in the examples\field directory. Run the examples using the command:
java examples.field.ComboFields
This field has matchPickList
set to true
. Because users might have their own unique honorific (such as Lord or Count), you may want to add new entries to the pick list. To do this you would set matchPickList
to false
and write code to add the user's typed entry to the pick list. An example is shown in the section on Event Programming.
p.add(new JLabel("String JCComboField: ")); p.add(combo1 = new JCComboField()); // create the validator and set its properties JCStringValidator sv = new JCStringValidator(); String[] string_values = {"Mr.", "Mrs.", "Ms.", "Miss", "Dr."}; sv.setMatchPickList(true); sv.setAllowNull(true); sv.setPickList(new JCListModel(string_values)); // set the value model and validator combo1.setValueModel(new StringValueModel()); combo1.setValidator(sv);
![]() |
The displayList
property is useful whenever you wish to present the user with a selection of items that are internally associated with ordinal numbers, perhaps for database applications. Note that the associated string value is displayed in the field, not its numerical value, even when the field loses focus.
p.add(new JLabel("Integer JCComboField: ")); p.add(combo2 = new JCComboField()); // create validator and set its properties JCIntegerValidator iv = new JCIntegerValidator(); Integer[] integer_values = {new Integer(1), new Integer(2), new Integer(3), new Integer(4)}; String[] integer_display = {"apple", "banana", "orange", "pear"}; iv.setMatchPickList(true); iv.setAllowNull(true); iv.setPickList(new JCListModel(integer_values)); iv.setDisplayList(integer_display); // create the invalidinfo and set its properties JCInvalidInfo iii = combo2.getInvalidInfo(); iii.setInvalidPolicy(JCInvalidInfo.CLEAR_FIELD); // set the value model, validator, and invalidinfo combo2.setValueModel(new IntegerValueModel()); combo2.setValidator(iv); combo2.setInvalidInfo(iii);
![]() |
This example allows the user to choose an astrological sign. Since there are only 12 astrological signs, it makes sense that matchPickList
is set to true
.
p.add(new JLabel("Long JCComboField: ")); p.add(combo3 = new JCComboField()); // create validator and set its properties JCLongValidator lv = new JCLongValidator(); Long[] long_values = {new Long(1), new Long(2), new Long(3), new Long(4), new Long(5), new Long(6), new Long(7), new Long(8), new Long(9), new Long(10), new Long(11), new Long(12)}; String[] long_display = {"Aries", "Taurus", "Gemini", "Cancer", "Leo", "Virgo", "Libra", "Scorpio", "Sagittarius", "Capricorn", "Aquarius", "Pisces"}; lv.setPickList(new JCListModel(long_values)); lv.setDisplayList(long_display); lv.setMatchPickList(true); lv.setAllowNull(true); // set the value model and validator combo3.setValueModel(new LongValueModel()); combo3.setValidator(lv);
![]() |
In this example, the matchPickList
property is set to false
, so that the user is able enter a value not contained in the pick list.
p.add(new JLabel("Short JCComboField: ")); p.add(combo4 = new JCComboField()); // create the validator and set its properties JCShortValidator shv = new JCShortValidator(); Short[] short_values = {new Short((short)1), new Short((short)2), new Short((short)3), new Short((short)4)}; shv.setMatchPickList(false); shv.setAllowNull(true); shv.setPickList(new JCListModel(short_values)); // set the value model and validator combo4.setValueModel(new ShortValueModel()); combo4.setValidator(shv);
![]() |
Setting the display pattern in a combo field allows the user to see the context of the value in the drop-down list.
p.add(new JLabel("Byte JCComboField: ")); p.add(combo5 = new JCComboField()); // create the validator and set its properties JCByteValidator bytev = new JCByteValidator(); Byte[] byte_values = {new Byte((byte)10), new Byte((byte)20), new Byte((byte)30), new Byte((byte)40)}; bytev.setDisplayPattern("0 feet"); bytev.setEditPattern(""); bytev.setAllowNull(true); bytev.setPickList(new JCListModel(byte_values)); // set the value model and validator combo5.setValueModel(new ByteValueModel()); combo5.setValidator(bytev);
![]() |
This example uses the isCurrency
property to indicate the value is a currency amount. The pick list values are displayed in the default currency format for the present locale.
p.add(new JLabel("Double JCComboField (currency): ")); p.add(combo6 = new JCComboField()); // create validator and set validator properties JCDoubleValidator dv = new JCDoubleValidator(); Double[] double_values = {new Double(100), new Double(200), new Double(300), new Double(400)}; dv.setAllowNull(true); dv.setCurrency(true); dv.setPickList(new JCListModel(double_values)); // set value model and validator combo6.setValueModel(new DoubleValueModel()); combo6.setValidator(dv);
![]() |
This example shows how the invalidPolicy
, JCInvalidInfo.RESTORE_DEFAULT
forces the field to display the default value after the user attempts to enter an invalid number.
p.add(new JLabel("BigDecimal JCComboField: ")); p.add(combo7 = new JCComboField()); // create validator and set its properties JCBigDecimalValidator bdv = new JCBigDecimalValidator(); BigDecimal[] bigdecimal_values = {new BigDecimal(10.0), new BigDecimal(20.0), new BigDecimal(30.0), new BigDecimal(40.0)}; bdv.setDefaultValue(new BigDecimal(-1)); bdv.setAllowNull(false); bdv.setPickList(new JCListModel(bigdecimal_values)); // create the invalidinfo and set its properties JCInvalidInfo bdii = combo7.getInvalidInfo(); bdii.setInvalidPolicy(JCInvalidInfo.RESTORE_DEFAULT);
![]() |
The invalidPolicy
for this field forces it to clear when the user enters an invalid value.
p.add(new JLabel("Float JCComboField: ")); p.add(combo8 = new JCComboField()); // create the validator and set its properties JCFloatValidator fv = new JCFloatValidator(); Float[] float_values = {new Float((float)100.101), new Float((float)200.202), new Float((float)300.303), new Float((float)400.404)}; fv.setAllowNull(true); fv.setPickList(new JCListModel(float_values)); fv.setMatchPickList(true); // create the invalidinfo and set its properties JCInvalidInfo fii = combo8.getInvalidInfo(); fii.setInvalidPolicy(JCInvalidInfo.CLEAR_FIELD); // set the value model, validator, and invalidinfo combo8.setValidator(fv); combo8.setValueModel(new FloatValueModel()); combo8.setInvalidInfo(fii);
![]() |
You use this field and validator combination to display IP addresses.
p.add(new JLabel("JCIPAddress JCComboField: ")); p.add(combo9 = new JCComboField()); // create the validator and set its properties JCIPAddressValidator ipv = new JCIPAddressValidator(); JCIPAddress[] ip_values = new JCIPAddress[3]; ip_values[0] = new JCIPAddress("0.0.0.0"); ip_values[1] = new JCIPAddress("24.190.120.3"); ip_values[2] = new JCIPAddress("123.10.3.15"); ipv.setPickList(new JCListModel(ip_values)); // set value model and validator combo9.setValueModel(new IPAddressValueModel()); combo9.setValidator(ipv); combo9.setValue(new JCIPAddress("121.35.2.150"));
![]() |
The following code snippets are from PopupFields.java found in the examples\field directory. Run the examples with the command:
java examples.field.PopupFields
In this example you can spin the year, month and time fields and select the date from the calendar display. This field also uses the format
property to present the selected date and time in a suitable format.
p.add(new JLabel("Date Time JCPopupField: ")); p.add(popup1 = new JCPopupField()); // create the validator and set the validator properties JCDateTimeValidator dtv = new JCDateTimeValidator(); dtv.setAllowNull(true); dtv.setFormat("MMM d 'yy H:mm:ss"); // set the value model and validator popup1.setValueModel(new CalendarValueModel( Calendar.getInstance())); popup1.setValidator(dtv);
![]() |
Once the user selects the date, the value is displayed in the field with defaultDetail
set to JCValidator.LONG
and the casePolicy
set to JCValidator.UPPERCASE
.
p.add(new JLabel("Date JCPopupField: ")); p.add(popup2 = new JCPopupField()); // create the validator and set the validator properties JCDateValidator dv = new JCDateValidator(); dv.setAllowNull(true); dv.setDefaultDetail(JCDateValidator.LONG); dv.setCasePolicy(JCDateValidator.UPPERCASE); // set the value model and validator popup2.setValueModel(new DateValueModel(new Date())); popup2.setValidator(dv);
![]() |
The following code snippets are from LabelFields.java found in the examples\field directory. Run the examples using the command:
java examples.field.LabelFields
This example demonstrates the effect of using the mask property.
p.add(new JLabel("String JCLabelField: ")); p.add(label1 = new JCLabelField()); // create the validator and set its properties JCStringValidator sv = new JCStringValidator(); sv.setMask("(@@@)@@@-@@@@ Ext. @@@"); sv.setAllowNull(true); // set the value model and validator label1.setValueModel(new StringValueModel()); label1.setValidator(sv); label1.setValue("4165941026");
![]() |
This example demonstrates how the displayPattern
property determines the format of the field.
p.add(new JLabel("Integer JCLabelField: ")); p.add(label2 = new JCLabelField()); // create validator and set its properties JCIntegerValidator iv = new JCIntegerValidator(); iv.setAllowNull(true); iv.setDisplayPattern("0 inches"); // set the value model and validator label2.setValueModel(new IntegerValueModel()); label2.setValidator(iv); label2.setValue(new Integer(100));
![]() |
This field displays a long value.
p.add(new JLabel("Long JCLabelField: ")); p.add(label3 = new JCLabelField()); // create validator and set its properties JCLongValidator lv = new JCLongValidator(); lv.setAllowNull(true); // set the value model and validator label3.setValueModel(new LongValueModel(new Long(1000000000000l))); label3.setValidator(lv);
![]() |
This example illustrates the use of a validator to provide a visual warning to the user when the field is invalid, that is when the field contains a value that is out of the set acceptable range.
p.add(new JLabel("Short JCLabelField: ")); p.add(label4 = new JCLabelField()); // create the validator and set its properties JCShortValidator shv = new JCShortValidator(); shv.setAllowNull(true); shv.setRange(new Short((short)0), new Short((short)10)); // set the invalid info properties JCInvalidInfo shii = text4.getInvalidInfo(); shii.setInvalidBackground(Color.red); // set value model, validator, and invalidinfo label4.setValueModel(new ShortValueModel(new Short((short)10))); label4.setValidator(shv); label4.setInvalidInfo(shii);
![]() |
This example shows how the invalidPolicy
, JCInvalidInfo.RESTORE_DEFAULT
forces the field to display the default value after the field receives a number that is out of range.
p.add(new JLabel("Byte JCLabelField: ")); p.add(label5 = new JCLabelField()); // create the validator and set its properties JCByteValidator bytev = new JCByteValidator(); bytev.setDefaultValue(new Byte((byte)5)); bytev.setAllowNull(true); bytev.setRange(new Byte((byte)1), new Byte((byte)10)); // set the invalidinfo properties JCInvalidInfo byteii = text5.getInvalidInfo(); byteii.setInvalidPolicy(JCInvalidInfo.RESTORE_DEFAULT); // set the value model, validator, and invalidinfo label5.setValueModel(new ByteValueModel(new Byte()); label5.setValidator(bytev); label5.setInvalidInfo(byteii); label5.setValue(new Byte("11"));
![]() |
The double validator associated with this text field is augmented by the isCurrency
property so that the value is treated as currency. The display format uses the currency format associated with the current locale.
p.add(new JLabel("Double JCLabelField (currency): ")); p.add(label6 = new JCLabelField()); // create validator and set its properties JCDoubleValidator dv = new JCDoubleValidator(); dv.setAllowNull(true); dv.setCurrency(true); // set value and validator label6.setValueModel(new DoubleValueModel(new Double(100.00))); label6.setValidator(dv);
![]() |
This field displays a BigDecimal type value.
p.add(new JLabel("BigDecimal JCLabelField: ")); p.add(label7 = new JCLabelField()); // create validator and set its properties JCBigDecimalValidator bdv = new JCBigDecimalValidator(); bdv.setAllowNull(true); // set the value model and validator label7.setValueModel(new BigDecimalValueModel()); label7.setValidator(bdv); label7.setValue(new BigDecimal("100000000.000000000000"));
![]() |
This field displays a float data type value.
p.add(new JLabel("Float JCLabelField: ")); p.add(label8 = new JCLabelField()); // create the validator and set its properties JCFloatValidator fv = new JCFloatValidator(); fv.setAllowNull(true); // set the value model and validator label8.setValidator(fv); label8.setValueModel(new FloatValueModel()); label8.setValue(new Float("1000.0000"));
![]() |
This example shows date and time values. The setValue()
method gives the field the current date and time as its initial value.
p.add(new JLabel("DateTime(Calendar) JCLabelField: ")); p.add(label9 = new JCLabelField()); // create validator and set its properties JCDateTimeValidator dtv = new JCDateTimeValidator(); dtv.setMaskInput(true); dtv.setAllowNull(true); // set value model and validator label9.setValueModel(new CalendarValueModel()); label9.setValidator(dtv); label9.setValue(Calendar.getInstance());
![]() |
The format
property for date and time validators is useful for presenting the value of the field in a way that is familiar to a specific group of users.
The format
property works as a mask for Date/Time validators. The field also uses the casePolicy
property to convert all characters to uppercase.
p.add(new JLabel("Date JCLabelField: ")); p.add(label10 = new JCLabelField()); // create the validator and set its properties JCDateValidator datev = new JCDateValidator(); datev.setMaskInput(true); datev.setFormat("MMMM d 'yy"); datev.setCasePolicy(JCDateValidator.UPPERCASE); // set value model and validator label10.setValueModel(new DateValueModel()); label10.setValidator(datev); label10.setValue(new Date());
![]() |
You use this field and validator combination to display and update time information. You can maintain a running clock if you wish. One way is to start a thread that sleeps for one second, then fires an event. You catch the event and update the time field using setValue().
This example shows the defaultDetail
's FULL
setting.
p.add(new JLabel("Time JCLabelField: ")); p.add(label11 = new JCLabelField()); // create the validator and set its properties JCTimeValidator timev = new JCTimeValidator(); timev.setMaskInput(true); timev.setDefaultDetail(JCTimeValidator.FULL); timev.setAllowNull(false); // set value model and validator label11.setValueModel(new DateValueModel()); label11.setValidator(timev); label11.setValue(new Date());
![]() |
You use this field and validator combination to display IP addresses.
p.add(new JLabel("JCIPAddress JCLabelField: ")); p.add(label12 = new JCLabelField()); // create the validator and set its properties JCIPAddressValidator ipv = new JCIPAddressValidator(); // set value model and validator label12.setValueModel(new IPAddressValueModel()); label12.setValidator(ipv); label12.setValue(new JCIPAddress("121.35.2.150"));
![]() |
A class can be notified both before and after a field's value has changed by implementing com.klg.jclass.util.value.JCValueListener
and registering itself with the field via addValueListener()
. In this code snippet, fieldInstance
is an instance of an editable JCComboField
with String validator. If the user types a new value into the field instead of choosing one of the values in the combo field, the code shown below adds the new information to the pick list.
First, the line of code that registers the field with the listener:
fieldInstance.addValueListener
(new AddPosition());
class AddPosition implements JCFieldListener { /** * Invoked just before the internal value of the field is changed */ public void valueChanging(JCValueEvent e) {} /** * Invoked just after the internal value of the field has been changed */ public void valueChanged(JCFieldEvent e) { if (e.getPosition() == e.NOT_FOUND) { JCField field = (JCField) e.getSource(); String oldpicklist[] = (String[])field.getPickList(); String newpicklist[]; // Add the string to the picklist if not null or not empty string if (e.getValue() == null || e.getValue().equals("")) { newpicklist = oldpicklist; else { newpicklist = new String[oldpicklist.length + 1]; for (int i = 0; i < oldpicklist.length; i++) { newpicklist[i] = oldpicklist[i]; newpicklist[oldpicklist.length] = (String) e.getValue(); } field.setPickList(newpicklist); } }
To listen for a change in state, use addPropertyChangedListener()
and listen for changes to the state
property.
[Home] [Contents] [Index] [Javadoc] | Comments on the documentation? Please send them to jclass_docs@klgroup.com |
[Previous] [Next] | |
[Frames] [No Frames] |