File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Swing:listeners Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Swing:listeners" Watch "Swing:listeners" New topic
Author

Swing:listeners

anuj thite
Ranch Hand

Joined: Apr 14, 2009
Posts: 49
Hello all,

i am new to java swing programming.

i am making small aplication in swing.

i am making listener classes for components mounted on frame.

my query is that can i make seperate listener classes & put them
in one package so that they are seperated from view classes (GUI Part)
which are preent in other package
or
make this listener classes as innner classes in classes which represent view part of an application.

which is standard way?

Please Help.

Thanks in advance.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> which is standard way?

there is no 'standard' way.

it depends entirely on the application
e.g. a 'Cancel' button, which has a single purpose, is ideally linked to an inner class listener,
whereas the buttons of (say) a minesweeper game would probaly share a class, using getSource()
to identify the button
Leandro Coutinho
Ranch Hand

Joined: Mar 04, 2009
Posts: 417
Hi.

I think you should use them as inner classes, because this is one of its purporses.
Normally it is a specialized code, i.e., you won't use it in other places, so you use it as inner class.

http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html
http://www.javaworld.com/javaworld/javaqa/2000-03/02-qa-innerclass.html?page=1
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Leandro Coutinho wrote:I think you should use them as inner classes, because this is one of its purporses.
Normally it is a specialized code, i.e., you won't use it in other places, so you use it as inner class.

As noted in the post above you, this is fine for small apps, but if you start to develop large and complex apps, you may find that this doesn't scale well, that you need to separate out the control portion from the view portion of the code.
anuj thite
Ranch Hand

Joined: Apr 14, 2009
Posts: 49
pete stein wrote:
Leandro Coutinho wrote:I think you should use them as inner classes, because this is one of its purporses.
Normally it is a specialized code, i.e., you won't use it in other places, so you use it as inner class.

As noted in the post above you, this is fine for small apps, but if you start to develop large and complex apps, you may find that this doesn't scale well, that you need to separate out the control portion from the view portion of the code.



Hi,

thanks alot for your suggestion.

but for seperating view portion from control portion, i have to make two separate packages.
1. one for classes related to view part of an application.
2. other for listener classes.

so for accessing listener class in view portion, i have to make it public.

so is it a Standard way to expose listener classes to outside according to application design point of view???





Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

I usually prefer to do it this way.
1) If the action is going to be invoked only from one source, have an anonymous inner class.
2) If the action is going to be shared (e.g. in a popup menu, a button and say menu bar) I prefer to subclass AbstractAction and share it between all the action sources.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Swing:listeners
 
Similar Threads
Controller and Bussiness delegate
Listeners the pro way...
FBNS: Package structure review
MVC with two Model interfaces
GUI design and future extensions...