A common operation in your components' Java code is to query or set the properties for other components on that page. Although you might have defined initial values for those properties in the IDE when you designed the page, standard property methods, called getters and setters, enable you to access those properties from your Java code. Every standard component has a getter or setter method for each property. A read-only property has a getter but not a setter. If you add a new property to a component, the IDE automatically adds the getter and setter methods for you.
A getter returns the value of a property. To get the value of a property, use get
with the name of the property, and capitalize the
initial letter of that property name. For example, to get the value of the value
property
of a component, use the getValue
method. For example, the following code gets the value
property
from an Output Text component:
outputText1.getValue();
Similarly, to get the values of the title
or style
property for a component, use the
getTitle
or getStyle
method. Each component property has a corresponding
getter method that returns the value of that property.
Many of the getter methods for properties return a String
object. The getValue
method
is an exception: it returns an object of class Object
. Depending on what you want to
do with that object (assign it to some other property, modify its value, and so on),
you might want to cast it to some other class. Casting converts an object of one class
into an instance of some other class.
For example, to cast the Object
contained in the value
property of a Text Field
component to an instance of class String
, use the following code:
String name = (String)textField1.getValue();
The Properties window can show you the class of the object that each property expects.
The tool tip for each property shows the longer descriptive name of the property as well
as the class name (for example Object
, String
, Boolean). You can also discover the class
that each getter method returns in the Java Editor from the code completion hints.
Properties that contain Boolean
values can be queried with this
and the name of the property.
For example, the escape
property for text-related components or the disabled
property are
Boolean
. They can have values of true or false, and they can be queried with the
isEscape
and isDisabled
methods. Boolean
getter methods return a
Boolean
value (true or false)
and can be used in tests, as shown in the following example:
if (button1.isDisabled()) {
// test the disabled property
// if the condition is true, then do this
// ...
} else {
// otherwise do this
// ...
}
// end if
A setter lets you change the value of a property. To set the value of any property, use a
method consisting of the word set
with the name of
that property, and capitalize the name of the property. Setter methods require a value to
set the property to an Object
, a String
, or a Boolean
, depending on the property. Here are
some examples:
textField1.setValue("Hello, world!");
outputText1.setValue(textField2.getValue());
button1.setDisabled(true);