I have a Java project that is getting to be relatively large (a few thousand lines in all, I'd say). It's definitely a lot larger than what I've dealt with before, being that I am still a relatively new programmer.

I have a class structure with my swing components and other classes I have made that, more and more, is becoming confusing. Even as a beginner, I can tell this wouldn't be considered "good programming practice". It's somewhat difficult to explain, and I cannot simply copy/paste code, as this is really dealing with the whole program. But I will do my best to explain...

As an example, I have a "Minion" class, and Minion objects will sometimes do something that needs to update a "levelGUI", an extended JPanel. How do they access this levelGUI? I will tell them something like "Game.getLevelGUI().someMethod()" and that just looks sloppy. Am I wrong?

The problem is that I have gotten ahead of myself in actual programming, relative to my understand of how the structure of said program works. I think what I will end up with, if I continue down this path, is a program that works as I intended, but with poor readability and great difficulty in altering down the road.

If this seems a difficult question to answer, links to articles which could explain how a large hierarchy of objects effectively "interact" or something to that effect... would be VERY helpful (I'm not being lazy, I have looked and looked, trying to find something).

  • 1
    The other way around is better: Have your LevelGUI class inspect your Minion object and then draw itself accordingly. So your Minion never actually have to deal with its representation. Or you can have a third class mediate between the gui and the minion as a controller. Apr 25, 2011 at 15:48

3 Answers 3


My all-time favorites resources about organizing guis are by Martin Fowler



and the patterns described there


I have had a similar experience when trying to do a Swing project after a long long time. Having developed web applications I can see some patterns right away to solve these kinda issues, but in Swing everything was looking blur.

Few things I like to share from my experience.

  • I followed MVC. Think of moving navigation control and stuff to controllers.
  • I followed Mediator, in order to decouple different controllers. Precisely, they are there providing a way of communication between controllers.
  • I have also spotted a use of Observer pattern in my project. You should read that too, and then see if something can be solved using that.

Few things you can consider step by step with refactoring your codes. As based on the short idea about your project, Here are short guidelines that make it a little better rather than rewrite your codes a lot.

  1. Try to separate your GUI codes out and then within that you could think about further improvement such as the model view controller model.

  2. Try to move the common class used for utilities to the utility module and import that jar to the required module.

  3. If any database or core coding those could be moved as a core module.

  4. Try to make use of the java package naming standard way to group the classes.

  5. Add more comments and improve readability with coding standards tools,

And if possible analysis of your project codes with Sonar: http://www.sonarsource.com/ guide you move to improve.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy