• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Break this code down in multiple classes

 
Ranch Hand
Posts: 34
Eclipse IDE Python Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!

 
Sheriff
Posts: 7113
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I am a man of mystery. Mostly because of this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic