File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ActionListener advice

 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a form (actually a JPanel) that will in some cases get called to stand alone and other times be part of a larger form. It has two buttons that I want to behave differently when it is part of a larger form. My instinct is to loop through the array of actionListeners for each of these buttons and remove them, then add different ones.

My question is: Would this be the proper way of doing this? If not, what is a better way?

Tim
 
Greg Fidster
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe would be smoother to have the JFrame/JDialog thats inserting the JPanel as part of its application be responsible for placing the actions onto the buttons. Instead of having the JPanel including any logic for the actions, include public setters for each of them, and thus remaining a pleasantly dumb gui. The actions themselves can be placed in separate classes and extend the AbstractAction class. The form or larger form can then choose the appropriate action classes and attach them to the buttons via the setters.
hth
cheers
 
Swati Udas
Ranch Hand
Posts: 121
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
U can add a addActionListener function to your JPanel
public addActionListener(Listener l){
button1.addActionListener(l);
}
U can call this function from the outer JFrame and add appropriate listener.
And u can add a default listener in the constructor of JPanel..fro standalone functionality!!
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like it. I can control listeners from outside the panel and set them more appropriately. That saves me from the messy business of removing them and adding others. Thank you.
 
Swati Udas
Ranch Hand
Posts: 121
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow! It worked!!
Great..thanks a lot for the reply
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic