• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Swing:listeners

 
anuj thite
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic