|
From: Mark R. D. <mdi...@la...> - 2003-08-13 20:04:26
|
In general I see two different policies going on in Controller when it
comes to buttons The first one adds the listener to the button in its
method, the second doesn't. I wonder if adding a listener twice
generates two events to that listener? If not then it would be wise to
set the listener in all Button methods.
/**
* Adds a user defined button to the toolbar. This must be called
* in the model's setup method or the button will not be added.
*
* @param label the label for the new JButton
* @param listener the ActionListener fired when the button is clicked
*/
public JButton addButton(String label, ActionListener listener) {
JButton b = new JButton(label);
int width = b.getPreferredSize().width - 10;
b.setMinimumSize(new Dimension(width, 31));
b.setMaximumSize(b.getMinimumSize());
b.setPreferredSize(b.getMinimumSize());
b.addActionListener(listener);
userButtons.add(b);
return b;
}
/**
* Adds a user defined button to the toolbar. This must be called
* in the model's setup method or the button will not be added.
*
* @param b the JButton to add
*/
public JButton addButton(JButton b) {
userButtons.add(b);
return b;
}
This silly thing is that the behaviors in these methods can easily be
attained view code, why create all these methods to support something
Swing already does naturally.
JButton b = new JButton(new ImageIcon(path));
b.addActionListener(listener);
controller.addButton(b);
JButton b = new JButton("Foobar");
b.addActionListener(listener);
controller.addButton(b);
JButton b = new JButton("Foobar", new ImageIcon(path));
b.addActionListener(listener);
controller.addButton(b);
I wonder if its good to create many different methods to support button
making in the controller when it can be done so easily (and with
ultimate flexibility) externally?
-Mark
Vos, Jerry R. wrote:
> In Controller.addIconButton(String path, ActionListener l)
>
> The actionlistener is never added to the created button.
>
>
>
> Original:
>
> *public* JButton addIconButton(String path, ActionListener l) {
>
> JButton b = *new* JButton(*new* ImageIcon(path));
>
> userButtons.add(b);
>
> *return* b;
>
> }
>
>
>
> fixed:
>
> *public* JButton addIconButton(String path, ActionListener l) {
>
> JButton b = *new* JButton(*new* ImageIcon(path));
>
> *b.addActionListener(l);*
>
> userButtons.add(b);
>
> *return* b;
>
> }
>
>
>
> Jerry Vos
>
> ANL
>
|