Is this possible in Java?

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2007
Posts: 81
Reputation: it2051229 is an unknown quantity at this point 
Solved Threads: 1
it2051229 it2051229 is offline Offline
Junior Poster in Training

Is this possible in Java?

 
0
  #1
Apr 5th, 2008
am creating a calculator using net beans IDE so yeah i made 0 - 9 buttons.. although seems like i dont want to create an event handler on each number cause it's like a waste of time.. is it possible that the zero to nine button can only trigger one event?

if it is possible, can you show me how?? annndd also I would like to know which number triggered the button
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 801
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Practically a Posting Shark

Re: Is this possible in Java?

 
0
  #2
Apr 5th, 2008
Yes, just create an inner class that implements ActionListener. Then in the actionPerformed( ActionEvent e ) method you can call e.getSource() to find the button that was pressed and act accordingly. Then just add a single instance of your inner class to each button.

Hope this helps
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Is this possible in Java?

 
0
  #3
Apr 5th, 2008
doesn't even have to be an inner class
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 224
Reputation: bugmenot is an unknown quantity at this point 
Solved Threads: 31
bugmenot bugmenot is offline Offline
Posting Whiz in Training

Re: Is this possible in Java?

 
0
  #4
Apr 5th, 2008
inner classes are nice because they can implicitly access the fields of the enclosing instance, so if your buttons were pointed to by a field, then you can use an inner class to access them
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Is this possible in Java?

 
0
  #5
Apr 5th, 2008
they're also a problem in that they prevent decoupling of responsibilities.

When they are short they're fine, but go to top level classes when they get longer than a few lines.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC