Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

recommended style for ActionListeners

Status
Not open for further replies.

jnicho02

Programmer
Jul 20, 1999
397
GB
In a simple situation like a number of JButtons on a JPanel there are two ways to implement an ActionListener.

1. on the panel
[tt]class MyPanel extends JPanel implements ActionListener[/tt]
then set action commands for each button, add actionListener(this) to each button, and in the actionPerformed() work out which button was pressed by looking at the ActionEvent's action command.

2. on the buttons
add a seperate ActionListener to each button with the code that it has to run

Personally I prefer the second method as the action is attached to the button that triggers it. Are there other considerations involved? or is it just a matter of personal style? [sig]<p> <br><a href=mailto: > </a><br><a href= home</a><br> [/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top