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?

