GUI Design Question

Hello, I wonder if you can give me some advice...

I created a small application about 3 years ago now and I thought I would give it a re-write. I am building the GUI part and I'm torn between two ways of implementing it:-

One way of building this GUI is by declaring all its controlling components in one place, as below:

publicclass Controllerimplements ActionListener{

privatefinalstatic String UNDO_ACTION ="UNDO";

privatefinalstatic String REDO_ACTION ="REDO";

JMenuItem undoItem = JMenuItem("undo");

JMenuItem redoItem = JMenuItem("redo");

JButton undoButton =new JButton("undo");

JButton redoButton =new JButton("redo");

public Controller(){

undoItem.setActionCommand(UNDO_ACTION);

redoItem.setActionCommand(REDO_ACTION);

undoButton.setActionCommand(UNDO_ACTION);

redoButton.setActionCommand(REDO_ACTION);

undoItem.addActionListener(this);

redoItem.addActionListener(this);

undoButton.addActionListener(this);

redoButton.addActionListener(this);

FrameTemplate frame =new FrameTemplate();

frame.MenuBar.add(undoButton);

frame.MenuBar.add(redoButton);

frame.ButtonPanel.add(undoButton);

frame.ButtonPanel.add(redoButton);

...

}

publicvoid actionPerformed( ActionEvent e ){

if (e.getActionCommand().equals("UNDO")){

...

}elseif (e.getActionCommand().equals("REDO")){

...

}

}

In the above code snippet, FrameTemplate is a generic stateless GUI "template" providing the ability to build a 'frame' in a few simple steps... it makes it easy to build a menu bar, a button panel and even makes building a status bar a quick activity.

The advantage of the above approach is that all the mediated components are declared in the one class and their interactions are controlled with ease. This FrameTemplate class can then be used to generate each frame in the application -- all that each FrameTemplate will need is a 'Controller' class, like the one above for example. This decouples the controlling logic from the stateless GUI.

I particularly like this first idea (even though it does not necessarily conform to some design pattern - or does it?)

The opposite approach to a generic GUI template is to let each component build itself specific to the application. The following code example shows how this may be achieved:

publicclass ButtonPanelimplements ActionListener{

privatestaticfinal JPanel BUTTON_PANEL =new JPanel();

privatestaticfinal JButton UNDO_BUTTON =new JButton("Undo");

privatestaticfinal JButton REDO_BUTTON =new JButton("Redo");

public buttonPanel(Mediator mediator){

BUTTON_PANEL.add(UNDO_BUTTON);

BUTTON_PANEL.add(REDO_BUTTON);

UNDO_BUTTON.addActionListener(this);

REDO_BUTTON.addActionListener(this);

mediator.registerUndoButton(UNDO_BUTTON);

mediator.registerRedoButton(REDO_BUTTON);

}

publicvoid actionPerformed( ActionEvent e ){

if (e.getActionCommand().equals("UNDO")){

mediator.undo();

}elseif (e.getActionCommand().equals("REDO")){

mediator.redo();

}

}

The above style would require an application specific class (non-generic) for each part of the GUI, i.e. the menu bar, button panel, content pane, status bar etc...

Which would be the best approach, a generic GUI template or hard coded containers? What method have you favored in your applications?

[5824 byte] By [marek.ja] at [2007-10-2 13:53:24]
# 1
Neither is correct. Anytime you've got a bigass "if .. else if .. else if .." block you're doing something wrong. Use Actions for large operations, and anonymous inner classesfor small ones.
es5f2000a at 2007-7-13 11:55:52 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

I did consider using Actions. But, since my application is small and will remain small [and will not need to scale well] using actions, in my opinion, would be over-kill. Anonymous inner classes too, will create more class files, which in turn, influences loading time and introduces unnecessary complexities. The actionPerformed method will at most have two 'if ... else ...' blocks. As such, I think I will define this method in the Controller class (as above).

Thank you for your reply.

marek.ja at 2007-7-13 11:55:52 > top of Java-index,Other Topics,Patterns & OO Design...