Length of Java source files?
Hello Everyone
I apologize in advance for asking yet another silly question... :eek: (a multitude of silly questions, actually :( )
I am currently writing a GUI that uses JTabbedPane with about five tabs and something like about 20 widgets on each tab. I am manually writing this GUI using GridBagLayout() and GridBagConstraints(). This GUI also has drop-down menus, a toolbar and a statusbar.
Now, the problem that I am discovering is that this source file is getting really big. Currently, it is around 1900 lines and I still have another three tabs to write. 90% of this code is GUI related as all the functionality is, as it should be, outside of the class.
I estimate that when finished this GUI class could have something of the order 6000 lines of code. Is this acceptable? What are your feelings about the maximum lines of code a Java source file should have?
I have been thinking about making each pane a separate class. Is that a good idea?
EDIT: Sorry forgot to say that the GUI is designed using SWING components.
Many Thanks
Nikki
Re: Length of Java source files?
The biggest problem I have encountered with huge classes such as this is maintenance. The bigger the beast becomes, the harder it will be to incorporate changes, additions, removals, etc...organizing your code into appropriate classes, methods, etc...really helps manage this. Its very situation dependent, so its difficult to give specific advice other than the above, and make use of interfaces to loosely couple things together.
Re: Length of Java source files?
Re: Length of Java source files?
For that type of setup, I usually start off with making each Tab its own class that might setup a JPanel. Then from a main class, I set up the JTabbedPane and create an instance of each tab, then add each tab's JPanel to the JTabbedPane. The main class is usually relatively short.
As the tabs become more complicated, each tab might then have its own package, and then within that package, I'll further break the tab class into separate classes that set up a single part of the tab. And that process of breaking down pieces into separate classes that do less at a time can continue.
Re: Length of Java source files?
The good thing about Java is that you can break apart large files like the one you are writing into multiple files easily via copy/paste. Java was designed to be an object-oriented language, so there is little benefit from putting all of your code and multiple classes into one file. You may want to implement a Model View Controller setup where the Model controls the logic and the View/Controller controls the GUI. A two layered MVC M-VC is usually used in Java since the Java GUI already has mappings for control and listeners for actions.
Model?view?controller - Wikipedia, the free encyclopedia