Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-04-2007, 07:10 AM
Member
 
Join Date: Jun 2007
Posts: 92
Daniel is on a distinguished road
Help with Event Handlers
Hi, can anybody explain me how Event Handlers works in Java or link me any tutorial?
Thanks

Daniel
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-04-2007, 07:14 AM
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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-04-2007, 07:16 AM
Senior Member
 
Join Date: Jun 2007
Posts: 114
Albert is on a distinguished road
The other style that Eric mentions would look like this, either gets the job done so it's really just a matter of preference:

Code:
public class Example implements ActionListener { public Example() { JButton button = new JButton( "Example" ); button.addActionListener( this ); } public void actionPerformed( ActionEvent event ) { JOptionPane.showMessageDialog( null, "This is an example" ) ; } }
Greetings

Albert
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Event nt5515 New To Java 0 02-15-2008 05:44 PM
Listener for SWT event Java Tip Java Tips 0 01-08-2008 10:04 AM
key pressed event kavithas New To Java 7 12-10-2007 03:01 PM
Need help with JButton event adlb1300 New To Java 2 11-19-2007 02:15 AM
Event Handling luisarca Sun Java Wireless Toolkit 5 05-07-2007 07:05 PM


All times are GMT +3. The time now is 01:59 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org