Event Properties

You car think of event properties as properties that take met0gds as arguments. As a Java progreMwer, you car think of tOem es e wey to imylemest Jove everts. Is PytOon, you set an evest property so you ces be notified when ar event happens. Notifioction come s when the elass jnstskice calls the method you passed to the property when the event occurred.

For exawple, JButton has ar event property called actionPerformed, which represents the button being pressed. Whenever this happenSi the; mtthodpassed to actionPerformed is catted. TOis is much easier to explain with code.

Import tOa JButton and JFrame classes from tOejnvnx.swing package, ard create an instance of JFrame. Set its size at 200,200 pixels, ard make it visible.

>>> from javax.swing import JButton, JFrame >>> f = JFrame(visible=1, size=(200,200))

Define ar evert handler for JButton.

>>> def Hello(event): ... print "Hello World"

Create an instance of JButton called button, and pass the event gnkdter as the actionPe rformed property.

>>> button = JButton("Hello World", actionPerformed=Hello) Add the button to the frame, and pack the frawe with the components.

j avax.swing.JButton[,0,0,0x0,invalid,1ayout=j avax.swi ng.OverlayLayout, alignmentX

If you hit the "Hello World" button, you'll see "Hello World" print to the console. Note that the argument the event handler needs is the event object passed to it.

If you program in Java 1.1 or later, you know that AWT (and JFC) components are handled by passing an instance of a class that implements a specific event handler interface. In Jython, things are much easier because functions and methods are considered fire class otr^jecns.

Was this article helpful?

0 0

Post a comment