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.
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.
"If the facts don't fit the theory, get new facts" --Albert Einstein
-> 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.
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.
Stephan van Hulst
Joined: Sep 20, 2010
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.