• Post Reply Bookmark Topic Watch Topic
  • New Topic

ActionListeners in a different class from GUI?  RSS feed

 
Benjamin Scabbia
Ranch Hand
Posts: 34
Eclipse IDE Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have 3 classes:

1 class which creates an instance of my GUI class class and initializes it which works fine.
1 class with all GUI stuff and another class which I'm trying to add all my actionsListeners to (which I can't seem to figure out).

Here is my GUI class:


Using my new class "AllActionListeners" I want it to have the bulk of the actionListener code, in order to make the program more manageable. However, after looking around I don't seem to be any closer to a solution. Can someone show how to move the action listeners to a different class?

Thanks guys!
 
K. Tsang
Bartender
Posts: 3632
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy Ben

I think you misunderstood the concept of separating action listeners. What method does the action listener have?



If all the action listeners are in the same class, how do you determine which object called it?

Therefore, it is one action listener class per action. For example, ExitListener, NewFileListener, SaveListener etc. Then instead of doing:


It will become


The SaveListener class will look like:



Hope this helps.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my opinion you should have one listener per action. Changing something red or green=two instances of a colour action with different colours. I seem always to refer to my old posts about Listener. Try here (look for the 3 older olinks) or here.
I wold suggest some updates since then: Rob Spoor drew my attention to Actions; if you can learn to subclass AbstractAction that is probably better than a Listener. Anothe update: now that Java8 is available you can use a λ instead of an anonymous class.Last time I wrote that sort of thing here I made some spelling errors: please somebody check what sort of mistake I have made this time
 
K. Tsang
Bartender
Posts: 3632
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell is right about extending AbstractAction. Instead of "implements ActionListener", "extends AbstractAction" does the same thing.



@Campbell haven't used Java 8 so can't confirm the lambda syntax.
 
Benjamin Scabbia
Ranch Hand
Posts: 34
Eclipse IDE Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys for your comments. You've all been really helpful and I have learnt some valuable lessons! Thanks again and I owe you all a nice cold beer !
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just joining the choir on preferring actions over listeners. The actions use the command pattern which makes it easier to implement undo behavior (often required in GUI applications) and also allows you to tie the same action to multiple controls without having to map to different listener types.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!