aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Adding more ActionListeners to buttons. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Adding more ActionListeners to buttons." Watch "Adding more ActionListeners to buttons." New topic
Author

Adding more ActionListeners to buttons.

Pete Tyo
Ranch Hand

Joined: May 11, 2005
Posts: 38
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

Joined: Apr 14, 2005
Posts: 78
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 ]

Teaching yourself anything is always the cheapest way, but it definitely takes a lot of time and effort.<br /> <br />Thank you javaranch <a href="http://"http://faq.javaranch.com/view?HowToAskQuestionsOnJavaRanch"" target="_blank" rel="nofollow">Learn How to Ask Your Question</a> and be nice
Pete Tyo
Ranch Hand

Joined: May 11, 2005
Posts: 38
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding more ActionListeners to buttons.