Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Length of Java source files?

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Length of Java source files?

    Hello Everyone

    I apologize in advance for asking yet another silly question... (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
    Last edited by ishtar; October 9th, 2011 at 09:40 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Length of Java source files?

    Thank you

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5

    Default 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
    Kenneth Walter
    Software Developer
    http://kennywalter.com

Similar Threads

  1. Source Code to Implement Circular Queue in Java
    By rainbow9 in forum Java Programming Tutorials
    Replies: 0
    Last Post: August 20th, 2011, 02:30 AM
  2. jsp/java based open source project
    By the light in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: July 17th, 2011, 03:24 AM
  3. Compiling many source files at once
    By mjpam in forum Java Theory & Questions
    Replies: 3
    Last Post: May 9th, 2011, 11:41 PM
  4. Implementing HTML tags in Java Source Code
    By bookface in forum Java Theory & Questions
    Replies: 4
    Last Post: March 19th, 2010, 09:29 PM
  5. Java Source/APIs to create a Fourm
    By softwarebuzz in forum Java Theory & Questions
    Replies: 2
    Last Post: January 9th, 2010, 01:46 AM