JClass Field

PreviousNextIndex

4

Example Code for Common Fields

Example Programs  Examples of Text Fields  Examples of Spin Fields  Examples of Combo Fields

Examples of Popup Fields  Examples of Label Fields  Event Programming


4.1 Example Programs

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;

Section 4.2.1, JCTextField with String Validator
 
Section 4.3.1, JCSpinField with String Validator
 
Section 4.6.1, JCLabelField with String Validator

Controlling the field's appearance before and during user edit;
Section 4.2.2, JCTextField with Integer Validator
 
Section 4.3.7, JCSpinField with BigDecimal Validator
 
Section 4.4.5, JCComboField with Byte Validator
 

Section 4.6.2, JCLabelField with Integer Validator

Selecting the contents of the field whenever it receives focus;
Section 4.2.3, JCTextField with Long Validator
 

Section 4.6.3, JCLabelField with Long Validator

Defining a range of valid input, and providing a visual and audio warning to the user when the field is invalid;
Section 4.2.4, JCTextField with Short Validator
 
Section 4.3.5, JCSpinField with Byte Validator
 

Section 4.6.4, JCLabelField with Short Validator

Defining a range of valid input, and a default value when the user's input is invalid;
Section 4.2.5, JCTextField with Byte Validator
 
Section 4.4.7, JCComboField with BigDecimal Validator
 

Section 4.6.5, JCLabelField with Byte Validator

Displaying the content of a field as a currency of a given locale;
Section 4.2.6, JCTextField with Double Validator
 
Section 4.3.6, JCSpinField with Double Validator
 
Section 4.4.6, JCComboField with Double Validator
 

Section 4.6.6, JCLabelField with Double Validator

Setting an invalid policy to restore the previous valid value;
Section 4.2.7, JCTextField with BigDecimal Validator
 

Section 4.6.7, JCLabelField with BigDecimal Validator

Setting an invalid policy to clear the field when the user's input is invalid;
Section 4.2.8, JCTextField with Float Validator
 
Section 4.3.2, JCSpinField with Integer Validator
 
Section 4.3.8, JCSpinField with Float Validator
 
Section 4.4.8, JCComboField with Float Validator
 

Section 4.6.8, JCLabelField with Float Validator

Allowing date input in several formats, and to attempt to complete a partially entered date;
Section 4.2.9, JCTextField with DateTime Validator
 

Section 4.6.9, JCLabelField with DateTime Validator

Allowing date input in one format, specified by place holder characters, and converting all characters to uppercase;
Section 4.2.10, JCTextField with Date Validator
 

Section 4.6.10, JCLabelField with Date Validator

Displaying and updating time information;
Section 4.2.11, JCTextField with Time Validator
 
Section 4.3.11, JCSpinField with Time Validator
 

Section 4.6.11, JCLabelField with Time Validator

Displaying IP addresses;
Section 4.2.12, JCTextField with IP Address Validator
 
Section 4.3.12, JCSpinField with IP Address Validator
 
Section 4.4.9, JCComboField with IP Address Validator
 

Section 4.6.12, JCLabelField with IP Address Validator

Associating numeric field values in the pick list with text displayed in the spin field;

Section 4.3.3, JCSpinField with Long Validator

Allowing the user to enter a value not contained in the pick list;
Section 4.3.4, JCSpinField with Short Validator
 
Section 4.4.1, JCComboField with String Validator
 

Section 4.4.4, JCComboField with Short Validator

Allowing the user to set date and/or time;
Section 4.3.9, JCSpinField with DateTime Validator
 
Section 4.5.1, JCPopupField with DateTime Validator
 

Section 4.5.2, JCPopupField with Date Validator

Creating date and time formats for a spin field;

Section 4.3.10, JCSpinField with Date Validator

Allowing the user to pick only a value from the pick list; any other input is cleared;
Section 4.3.3, JCSpinField with Long Validator
 

Section 4.4.3, JCComboField with Long Validator

Presenting the user with a choice of items internally associated with ordinal numbers, for example for database applications;

Section 4.4.2, JCComboField with Integer Validator


4.2 Examples of Text Fields

The following code snippets are from TextFields.java found in the examples/field directory. Run the examples using the command:
java examples.field.TextFields

4.2.1 JCTextField with String Validator

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

Figure 10 :  JCTextField with String validator.

4.2.2 JCTextField with Integer Validator

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

Figure 11 :  JCTextField with integer validator showing edit (top) and display formats.

4.2.3 JCTextField with Long Validator

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

Figure 12 :  JCTextField with long validator showing edit (top) and display formats.

4.2.4 JCTextField with Short Validator

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

Figure 13 :  JCTextField with short validator is given an invalid entry.

4.2.5 JCTextField with Byte Validator

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

Figure 14 :  JCTextField with byte validator showing default value.

4.2.6 JCTextField with Double Validator

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

Figure 15 :  JCTextField with double validator and currency set showing edit (top) and display formats.

4.2.7 JCTextField with BigDecimal Validator

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.111)));
text7.setValidator(bdv);
text7.setInvalidInfo(bdii);

Figure 16 :  JCTextField with BigDecimal validator.

4.2.8 JCTextField with Float Validator

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(-3033.32)));
text8.setInvalidInfo(fii);

Figure 17 :  JCTextField with float validator.

4.2.9 JCTextField with DateTime Validator

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

Figure 18 :  JCTextField with datetime validator showing two different edit formats.

4.2.10 JCTextField with Date Validator

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

Figure 19 :  JCTextField with date validator.

4.2.11 JCTextField with Time Validator

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

Figure 20 :  JCTextField with time validator showing default (top) and FULL display detail.

4.2.12 JCTextField with IP Address Validator

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

Figure 21 :  JCTextField with IP address validator.


4.3 Examples of Spin Fields

The following code snippets are from SpinFields.java found in the examples/field directory. Run the examples with the command:
java examples.field.SpinFields

4.3.1 JCSpinField with String Validator

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

Figure 22 :  JCSpinField with String validator.

4.3.2 JCSpinField with Integer Validator

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

Figure 23 :  JCSpinField with integer validator.

4.3.3 JCSpinField with Long Validator

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

Figure 24 :  JCSpinField with long validator.

4.3.4 JCSpinField with Short Validator

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

Figure 25 :  JCSpinField with short validator.

4.3.5 JCSpinField with Byte Validator

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

Figure 26 :  JCSpinField with byte validator.

4.3.6 JCSpinField with Double Validator

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

Figure 27 :  JCSpinField with double validator and currency set showing edit (top) and display formats.

4.3.7 JCSpinField with BigDecimal Validator

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

Figure 28 :  JCSpinField with BigDecimal validator showing edit (top) and display formats.

4.3.8 JCSpinField with Float Validator

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

Figure 29 :  JCSpinField with float validator.

4.3.9 JCSpinField with DateTime Validator

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

Figure 30 :  JCSpinField with datetime validator.

Note: The spin increment determines how many elements will be scrolled through for each spin.The spin increment can be set for a JCSpinField with Date validator to more than one, which is its default setting.

4.3.10 JCSpinField with Date Validator

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

Figure 31 :  JCSpinField with date validator.

Note: The spin increment determines how many elements will be scrolled through for each spin.The spin increment can be set for a JCSpinField with Date validator to more than one, which is its default setting.

4.3.11 JCSpinField with Time Validator

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

Figure 32 :  JCSpinField with time validator.

Note: The spin increment determines how many elements will be scrolled through for each spin.The spin increment can be set for a JCSpinField with Time validator to more than one, which is its default setting.

4.3.12 JCSpinField with IP Address Validator

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

Figure 33 :  JCSpinField with IP address validator.


4.4 Examples of Combo Fields

The following code snippets are from ComboFields.java found in the examples/field directory. Run the examples using the command:
java examples.field.ComboFields

4.4.1 JCComboField with String Validator

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 Section 4.7, 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);

Figure 34 :  JCComboField with String validator.

4.4.2 JCComboField with Integer Validator

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

Figure 35 :  JCComboField with integer validator.

4.4.3 JCComboField with Long Validator

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

Figure 36 :  JCComboField with long validator.

4.4.4 JCComboField with Short Validator

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

Figure 37 :  JCComboField with short validator.

4.4.5 JCComboField with Byte Validator

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


Figure 38 :  JCComboField with byte validator: dropdown list (top), edit format (middle), display format (bottom).

4.4.6 JCComboField with Double Validator

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

Figure 39 :  JCComboField with double validator.

4.4.7 JCComboField with BigDecimal Validator

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

Figure 40 :  JCComboField with BigDecimal validator.

4.4.8 JCComboField with Float Validator

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

Figure 41 :  JCComboField with float validator.

4.4.9 JCComboField with IP Address Validator

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

Figure 42 :  JCComboField with IP address validator.


4.5 Examples of Popup Fields

The following code snippets are from PopupFields.java found in the examples/field directory. Run the examples with the command:
java examples.field.PopupFields

4.5.1 JCPopupField with DateTime Validator

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

Figure 43 :  JCPopupField with datetime validator.

Note: If the format for the JCDateTimeValidator specifies the use of military hours (i.e. hours ranging from 0-23), the hour spinner in the popup field will also use military hours.

4.5.2 JCPopupField with Date Validator

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

Figure 44 :  JCPopupField with date validator.


4.6 Examples of Label Fields

The following code snippets are from LabelFields.java found in the examples/field directory. Run the examples using the command:
java examples.field.LabelFields

4.6.1 JCLabelField with String Validator

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

Figure 45 :  JCLabelField with String validator.

4.6.2 JCLabelField with Integer Validator

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

Figure 46 :  JCLabelField with integer validator.

4.6.3 JCLabelField with Long Validator

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(1000000000000)));
label3.setValidator(lv);  

Figure 47 :  JCLabelField with long validator.

4.6.4 JCLabelField with Short Validator

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

Figure 48 :  JCLabelField with short validator is given an invalid entry.

4.6.5 JCLabelField with Byte Validator

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(5));
label5.setValidator(bytev);
label5.setInvalidInfo(byteii);
label5.setValue(new Byte("11"));

Figure 49 :  JCLabelField with byte validator showing default value.

4.6.6 JCLabelField with Double Validator

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

Figure 50 :  JCLabelField with double validator and currency set.

4.6.7 JCLabelField with BigDecimal Validator

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.111"));

Figure 51 :  JCLabelField with BigDecimal validator.

4.6.8 JCLabelField with Float Validator

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

Figure 52 :  JCLabelField with float validator.

4.6.9 JCLabelField with DateTime Validator

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

Figure 53 :  JCLabelField with datetime validator.

4.6.10 JCLabelField with Date Validator

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

Figure 54 :  JCLabelField with date validator.

4.6.11 JCLabelField with Time Validator

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

Figure 55 :  JCLabelField with time validator.

4.6.12 JCLabelField with IP Address Validator

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

Figure 56 :  JCLabelField with IP address validator.


4.7 Event Programming

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, combo is an instance of an editable JCComboField with a JCStringValidator. 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:

combo.addValueListener(this);

Now the event handling code:

public void valueChanged(JCValueEvent e) {
// Gets the contents of the combo box's text field
  String newValue = ((String) combo.getValue()).trim();
  boolean found = false;
  int position = 0;
  // Make sure there is something in the text field
  if(newValue != null && newValue.length() > 0){
    for (int i = 0; i < dm.getSize(); i++){
    // See if the pick list contains an item matching the text field
    if (newValue.compareTo((String)dm.getElementAt(i)) == 0) {
      found = true;
    } else {
      // Set the insertion point for a new item
      if (newValue.compareToIgnoreCase(
         (String)dm.getElementAt(i)) > 0) {
        position = i + 1;
      }
     }
   }
  }
  // Add a new item to the data model
  if (!found && newValue != null && newValue.length() > 0) {
    dm.add(position, (String) newValue);
    combo.setPickList(dm);
    combo.setSelectedIndex(position);
  }
}

Items may be appended to the list in the combo field with autocomplete off or on. It is recommended that the append mode in autocomplete be turned off because end users may find interaction with the text field awkward.

Removing items from a JCComboField's list model does not require implementing the JCValueListener interface. All that is required is a reference to the combo box's list model and a core Java listener. For instance, if you provide a button that allows the end user to remove the item currently in the text field, the code might be something like:

JButton removeButton = new JButton("Remove Selection");
removeButton.addActionListener(this);
...
public void actionPerformed(ActionEvent e){
  String newValue = null;
if (combo.getValue() != null){
    newValue = ((String) combo.getValue()).trim();
}
boolean found = false;
int position = 0;
if(newValue != null && newValue.length() > 0){
    for (int i = 0;i < dm.getSize();i++){
    if (newValue.compareTo((String)dm.getElementAt(i)) == 0) {
      found = true;
      position = i;
      dm.removeElementAt(i);
      combo.setPickList(dm);
      combo.setValue("");
      break;
    }
  }
  }
}

The following figure shows how an item may be removed with the use of a button whose action listener uses the code shown above.

Figure 57 :  A combo field before and after the removal of an item.

The combo field shown on the left of Figure 57 illustrates the addition of an unwanted item. A user has typed the words "bad type" and has pressed the Enter key, thus adding the entry to the combo field's list model. Realizing the error, the user has pressed the Remove Selection button. The item in the text box is removed from the list, no longer appearing in the right-hand combo field of Figure 57.


PreviousNextIndex