Properties
JPython uses JavaBean properties to make it easier to interact with most
Java classes. These properties can be used as normal object attributes,
and can also be specified to the class constructor as keyword arguments
(this idea is stolen from TkInter where it seems to work extremely well).
These properties are generated automatically using the JavaBean Introspector
which identifies properties either from common design patterns, or from
explicitly specified BeanInfo.
As a first example, consider the case where you wish to create a button
that is disabled.
The first example shows how you would do this in the typical Java fashion:
b = awt.Button()
b.setEnabled(0)
The second example shows how enabled can be set as a property:
b = awt.Button()
b.enabled = 0
The final example sets this property at instantiation time using a keyword
argument:
b = awt.Button(enabled=0)
Tuples
If the value of a property is specified as a tuple, then the property will
be created by applying the constructor for the type of the property to
the tuple. This is particularly handy for specifying sizes:
frame = awt.Frame(size=(500,100))
It can also be handy for specifying color as an RGB triple:
frame.background = 255,255,0
will set the background color of the frame to yellow.
Event Properties
In standard Java, the event handlers for a widget are specified by passing
in an instance of a class that implements the appropriate interface.
This is the only reasonable approach to take in a language that doesn't
have first-class functions. In JPython, for every event listener
supported by a class, there will be a property added to the class for each
method supplied by the event listener class. These properties can
be set to give a function to be called when the appropriate event occurs.
The standard Java style for setting an event listener is shown below:
This can be written in a more Pythonesque (and compact) style by using
event properties as follows:
def exit(event):
java.lang.System.exit(0)
button = awt.Button("Close Me!", actionPerformed=exit)