Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding more ActionListeners to buttons.

 
Pete Tyo
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to create a program for myself while learning to use swing. I ran into a problem with ActionListeners... Here is what I have so far below.. I have questions in bold within the code between ***********

[ November 10, 2005: Message edited by: Pete Tyo ]
 
Nicholas Carrier
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if I have to use more than one button, I use inner clases.

For example

JButton buttonOne = new JButton("one");
JButton buttonTwo = new JButton("two");

buttonOne.addActionListener(new ButtonOneListener());
buttonTwo.addActionListener(new ButtonTwoListener());

// then later on add the two inner classes (note they have to be in the main class' brackets, hence "inner class").


// listener for Button One

class ButtonOneListner implements ActionListener {

// method that you tells the listener what to do
public void actionPerformed(ActionEvent event) {

// what the button does
System.out.prinln("You presseed button one");

} // end of method

} // end of inner class

// listener for Button Two

class ButtonTwoListner implements ActionListener {

// method that you tells the listener what to do
public void actionPerformed(ActionEvent event) {

// what the button does
System.out.prinln("You presseed button two");

} // end of method

} // end of inner class

Hope this helps, and is what you are looking for.
[ November 10, 2005: Message edited by: Nicholas Carrier ]
 
Pete Tyo
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nicholas,
Thankyou for your help. I was able to do as you suggested and all my buttons are working using inner classes.... Just have to figure out how to open and write to a random access file...

Thanks again,

Pete
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic