Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to organize a Swing project

 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

I am not that familiar with Swing or GUIs in Java, to fix that I have created a project for myself to help learn swing etc. I am running into the issue where I am getting more and more code; but I dont really know how I should organize Swing projects. Right now I have separated the creation of the user interface, and the event handler from the main program file, and this is my current package structure.

package structure:
jtexteditor
jtexteditor/ui
jtexteditor/event

I was wondering if organizing the way I am is a good idea or is there a better way? I am especially curious about how to separate the EventHandler from the main file because once I moved it out of the main file it stopped registering my events. I am still working on that.

any input would be great.
Thanks.
Hunter
 
Stephan van Hulst
Bartender
Pie
Posts: 5539
53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Hunter,

Usually, I don't put my GUI classes in separate packages, unless a particular component becomes very big.

I usually have a structure like this:

project
-> model/business logic

project/view
-> GUI components, event handlers, helper classes

project/view/mycomponent
-> extra classes belonging to MyComponent, MyComponent resides in the project/view package.
Only when MyComponent is very complex, otherwise they go in the view package as well.

This was inspired mostly by the way Swing organizes its classes. Take JTree or JFileChooser for example. They have a separate package for extra classes.
I only separate my event handlers from the class they belong to when they are general enough to be used by other classes as well. If the event handlers perform very specific operations relating to some component, best not to separate them.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cool thank you for your quick reply. I also have a couple more questions I hope that is ok.

1) Right now my main class file is listener for many of the events that occur within my program (since i integrated the event handler back into it), I was wondering if this was bad style.

2) I have a JMenuBar with menus on it, and each item in the menus corresponds to a method in my program, how do you general group those functions into classes? a class like FileMenuOperations seemed weird to me.


thanks.
Hunter.
 
Stephan van Hulst
Bartender
Pie
Posts: 5539
53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Always use a separate listener per action. Anonymous ActionListeners are great! For typical actions that a user can perform (usually in the form of menu items or tool bar buttons) I like to extend AbstractAction instead.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic