Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Handling of events question  RSS feed

 
Rick Reumann
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a Swing newbie and am having some difficulty getting down some architectural considerations in Swing applications (I'm used to MVC architecture in JSP/Servlets but having some questions now with Swing).
For example say you have a typical JMenuBar with one of the menus with File selections ( New, Load, Save, etc.). Now the way I have it set up for each menu item is:
fileMenu.add( new FileAction("New") );
fileMenu.add( new FileAction("Load") );
fileMenu.add( new FileAction("Save") );
Then of course I have a FileAction class:

What is the best way to manage events that are all similiar..like in this case File type operations..loading, saving, new, etc. Is it best to do it like I have above or maybe to create individual listeners for each type of action (load,save,etc)?
If someone could point to other Swing architecture links I'd appreciate it. Most of the tutorials I've looked at are very basic in the area of event handling. I'm looking for some examples of how to "fit it all together" in a real life application.
Thanks for any help.
Rick
[ February 16, 2002: Message edited by: Rick Reumann ]
 
Terence Doyle
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rick,
Handling the events that way looks fine. One thing to remember with button event handling is that the button will block until the action to be performed returns so if you want to do a lot of stuff with just one click you should keep your user informed in case it takes a while to do the work.
In particular you should watch out for io errors with files etc and be sure that if the loading / saving doesn't get done for some reason, the user is informed properly and the application returns to a usable state. This can be done by putting a JOptionPane.showMessage() dialog in the catch part for IOExceptions that informs the user and simply goes back to the application.
Hope that helps,
Terry
 
Daniel Searson
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I do this I prefer to create a new action for each one - ie. NewAction, SaveAction, SaveAsAction etc. Relying on strings to distribute events is a bit to error-prone for my liking...
 
Rick Reumann
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Daniel and Terrence, I appreciate you feedback.
Terence, thanks for you precaution about the buttons locking. I guess I'll probably have to spawn on another thread to file io stuff.
Daniel, that is what I was also concerned about from a design perspective. The problem I'm quickly running into is "organization" In other words, I want to code something that is easy to follow. So my next questions is what types of things should be seperate classes? In other words, maybe all the seperate Action classes you mention "SaveAction", "LoadAction", etc. should all be in a seperate class file called "FileActions.java" or something like that? I'm just curious how you all manage to organize things inside the GUI application. It seems like it would be easy for things to get really big inside of one file.
thanks again
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!