View Single Post
  #2 (permalink)  
Old 07-04-2007, 08:14 AM
Eric Eric is offline
Senior Member
 
Join Date: Jun 2007
Posts: 111
Eric is on a distinguished road
Events in java are implemented via interfaces and/or inner class and interfaces.

For example, if you have a button and you wish to add an onclick event handler for that button, than you write:

Code:
JButton button = new JButton( "Example" ); button.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { JOptionPane.showMessageDialog( null, "This is an example" ) ; } } );
In the above sample, we used an inner class to handle button clicks on the button instance of JButton. There are other events such as on focus etc...that can be implemented for JButton instances.

If you want to implement an interfaces without declaring inner classes, than your main class must implement ActionListener interface and defines an actionPerformed method.

For a good introductory tutorial on Events in Java, check out the Sun Microsystems tutorial...which provides a good hands on guide on how to implements events in java.

http://java.sun.com/docs/books/tutor...nts/index.html

Greetings.

Eric
Reply With Quote