Java GUI design

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2007
Posts: 2
Reputation: rwm is an unknown quantity at this point 
Solved Threads: 0
rwm rwm is offline Offline
Newbie Poster

Java GUI design

 
0
  #1
Oct 18th, 2007
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:

  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:

  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!

Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,493
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 519
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Java GUI design

 
0
  #2
Oct 18th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 2
Reputation: rwm is an unknown quantity at this point 
Solved Threads: 0
rwm rwm is offline Offline
Newbie Poster

Re: Java GUI design

 
0
  #3
Oct 19th, 2007
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!!!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC