1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
Title: Properties
<h3>Properties</h3>
Jython 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).
<P>These properties are generated automatically using the JavaBean
Introspector which identifies properties either from common design
patterns, or from explicitly specified BeanInfo.
<P>As a first example, consider the case where you wish to create a
button that is disabled.
<P>The first example shows how you would do this in the typical Java fashion:
<blockquote><pre>
b = awt.Button()
b.setEnabled(0)
</pre></blockquote>
The second example shows how enabled can be set as a property:
<blockquote><pre>
b = awt.Button()
b.enabled = 0
</pre></blockquote>
The final example sets this property at instantiation time using a keyword
argument:
<blockquote><pre>
b = awt.Button(enabled=0)
</pre></blockquote>
<h3>Tuples</h3>
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:
<blockquote><pre>
frame = awt.Frame(size=(500,100))
</pre></blockquote>
It can also be handy for specifying color as an RGB triple:
<blockquote><pre>
frame.background = 255,255,0
</pre></blockquote>
will set the background color of the frame to yellow.
<h3>Event Properties</h3>
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 Jython, 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.
<P>The standard Java style for setting an event listener is shown below:
<blockquote><pre>
class action(awt.event.ActionListener):
def actionPerformed(self,event):
java.lang.System.exit(0)
button = awt.Button("Close Me!")
button.addActionListener(action())
</pre></blockquote>
This can be written in a more Pythonesque (and compact) style by using
event properties as follows:
<blockquote><pre>
def exit(event):
java.lang.System.exit(0)
button = awt.Button("Close Me!", actionPerformed=exit)
</pre></blockquote>
<h3>Methods, Properties and Event Properties</h3>
Jython have only one namespace for these three class attributes.
Java can be seen as having a unique namespace for each of the three
types. As a consequense, there can be conflicts between methods,
properties and event properties.
These conflicts are resolved so that:
<blockquote>
properties < event-properties < fields < methods
</blockquote>
This means that a method will override a field with the same name.
Some carefull handling of properties and static fields allow for
the existence of, and access to, both an instance property and a
static field with the same name.
<p>
|