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:

//custom menu bar
public class MyMenuBar extends JMenuBar
{
	...
}

//custom generic menu. all
//look-and-feel is set
//here i.e. fonts etc
public class MyMenu extends JMenu
{
	...
}

//the file menu
public class FileMenu extends MyMenu
{
	...
}

//custom generic menu item. all
//look-and-feel is set here.
//i.e. fonts, tearable etc
public class MyMenuItem extends JMenuItem
{
	...
}

//the "new file" menu item
public class FileMenuNewMenuItem extends MyMenuItem
{
	...
}

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

//MyMenuBar is composed of menus
public class MyMenuBar extends JMenuBar
{
	//members
	private FileMenu fileMenu;

	//default constructor
	public MyMenuBar()
	{
		//create file menu
		fileMenu = new FileMenu();
		//add file menu to menu bar
		this.add(fileMenu);
	}
}

//File menu is composed of menu items
public class FileMenu extends MyMenu
{
	//members
	private FileMenuNewMenuItem newItem;

	//default constructor
	public FileMenu()
	{
		//set text and mnemonic
		super("File",'F');
		//create new file menu item
		newItem = new FileMenuNewItem();
		//add to menu
		this.add(newItem);
	}
}

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!

:)

Recommended Answers

All 2 Replies

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.

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!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.