944,025 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 3192
  • Java RSS
Oct 18th, 2007
0

Java GUI design

Expand Post »
Hiya!

I've got a problem writing GUI's. Basically the design portion in code.

What I've done is I've broken the GUI components into seperate classes, for example:

Java Syntax (Toggle Plain Text)
  1. //custom menu bar
  2. public class MyMenuBar extends JMenuBar
  3. {
  4. ...
  5. }
  6.  
  7. //custom generic menu. all
  8. //look-and-feel is set
  9. //here i.e. fonts etc
  10. public class MyMenu extends JMenu
  11. {
  12. ...
  13. }
  14.  
  15. //the file menu
  16. public class FileMenu extends MyMenu
  17. {
  18. ...
  19. }
  20.  
  21. //custom generic menu item. all
  22. //look-and-feel is set here.
  23. //i.e. fonts, tearable etc
  24. public class MyMenuItem extends JMenuItem
  25. {
  26. ...
  27. }
  28.  
  29. //the "new file" menu item
  30. public class FileMenuNewMenuItem extends MyMenuItem
  31. {
  32. ...
  33. }

and all the parent widgets are composed of the smaller widget items, like this:

Java Syntax (Toggle Plain Text)
  1. //MyMenuBar is composed of menus
  2. public class MyMenuBar extends JMenuBar
  3. {
  4. //members
  5. private FileMenu fileMenu;
  6.  
  7. //default constructor
  8. public MyMenuBar()
  9. {
  10. //create file menu
  11. fileMenu = new FileMenu();
  12. //add file menu to menu bar
  13. this.add(fileMenu);
  14. }
  15. }
  16.  
  17. //File menu is composed of menu items
  18. public class FileMenu extends MyMenu
  19. {
  20. //members
  21. private FileMenuNewMenuItem newItem;
  22.  
  23. //default constructor
  24. public FileMenu()
  25. {
  26. //set text and mnemonic
  27. super("File",'F');
  28. //create new file menu item
  29. newItem = new FileMenuNewItem();
  30. //add to menu
  31. this.add(newItem);
  32. }
  33. }

As you can see, adding FileMenuNewItem to FileMenu requires just a single line of code, all the actual code that composes the look and operations of the "new file" menu item is contained in the class FileMenuNewMenuItem...

I feel it's too much bloat...

Can someone suggest a better method of writing easy to maintain/extend GUI's?

Thanks!

Similar Threads
rwm
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rwm is offline Offline
2 posts
since Oct 2007
Oct 18th, 2007
0

Re: Java GUI design

Well, you are probably right on the bloat. It all depends on how your menu items need to be used. If you have a single menu for the app, then just create a single menu bar class that manages those items. If you need to share those menu items among a few other classes, you may want to use a MenuItemFactory to centralize the creation of the items and get them as needed to compose other menus.

Creating small classes that implement Action is a good way to encapsulate what a menu item does and those actions can also be used with popup menus and buttons.

It's really a matter of balancing your usage needs with the bloat that can occur by making every tiny thing a separate class. There isn't so much one "right" answer as there is a good flexible solution for your particular context... and that is where the "art" part of software development comes in.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Oct 19th, 2007
0

Re: Java GUI design

Hey thanks man! That's true!

It's a pretty complex GUI system, and I wanted to make it as easy as possbile to extend/modify...

I guess I went a little overboard on the classes, so now I'm gonna just make the components a little larger, i.e. all menu items on a single menu, so the menu is the smallest component. I suppose that's a reasonable way to go...

I've been looking at the MVC pattern, seems pretty good way to do things...

Yeah, GUI design is hard work! Not simple at all!!!
rwm
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rwm is offline Offline
2 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Please Help me only one day left for me...!
Next Thread in Java Forum Timeline: how to make a textfield in html form readonly in servlet





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC