• Post Reply Bookmark Topic Watch Topic
  • New Topic

Break this code down in multiple classes  RSS feed

 
Benjamin Scabbia
Ranch Hand
Posts: 34
Eclipse IDE Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this code which is already getting a bit 'heavy' to manage in a single class. Ideally, I want to break this code down in a few classes to make it easier to manage. add and edit. Can someone give me advice on what I should split in the current setup? THanks!

 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GUIs often have long methods. Still, say to yourself: what am I doing? I'm painting a GUI. How do I paint a GUI? First I create a menu. My events have callbacks. Should they be inline (very simple) or methods or Classes? you should try to separate the interface (not the Java Interface, but what the users sees) from what you do with the data.

Try this and post the code again.
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy

I see you are building a GUI.

With GUI, I tend to make classes or methods per component chunk. For example, menu bar can be in its own class with the menu items and stuff. The panels (depending on how complex you need) can be in their own classes. Even those action listeners can be in their own classes especially if they can be reuse.

However, you currently have most of the code in your constructor. Try separating the section of code by components and group these lines into a separate method/class. Then your constructor would look a bit neater.

Also even the main window (the class extends JFrame) can be on its own. This way your main method will just have 1 line eg new MyMainWindow();
 
Benjamin Scabbia
Ranch Hand
Posts: 34
Eclipse IDE Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:GUIs often have long methods. Still, say to yourself: what am I doing? I'm painting a GUI. How do I paint a GUI? First I create a menu. My events have callbacks. Should they be inline (very simple) or methods or Classes? You should try to separate the interface (not the Java Interface, but what the users sees) from what you do with the data.

Try this and post the code again.


Thanks for reply! Yes, indeed, GUI is already pretty big! Good ideas, I will see what I can do
 
Benjamin Scabbia
Ranch Hand
Posts: 34
Eclipse IDE Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:Howdy

I see you are building a GUI.

With GUI, I tend to make classes or methods per component chunk. For example, menu bar can be in its own class with the menu items and stuff. The panels (depending on how complex you need) can be in their own classes. Even those action listeners can be in their own classes especially if they can be reuse.

However, you currently have most of the code in your constructor. Try separating the section of code by components and group these lines into a separate method/class. Then your constructor would look a bit neater.

Also even the main window (the class extends JFrame) can be on its own. This way your main method will just have 1 line eg new MyMainWindow();


Howdy! I am indeed . Just tried to seperate the whole panel side (threw plenty of errors) so I will try a smaller chunk at a time starting from action listeners and get back here if I need further assistance. Thanks again for your help
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually if I would start with the menu bar. Have those menu bar and menu item lines separate out. Also since the action listeners are for the menu items then for the time being these can be put in the menu bar class too.

Yet you can start with action listeners.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!