Re: Button event
- From: Thomas Fritsch <i.dont.like.spam@xxxxxxxxxxx>
- Date: Thu, 29 Jun 2006 17:05:20 GMT
Steve W. Jackson schrieb:
In article <newscache$sidm1j$8v1$1@xxxxxxxxxxx>,[...]
Thomas Fritsch <i.dont.like.spam@xxxxxxxxxxx> wrote:
My code typically looks like this:
[...]private Action openAction = new OpenAction();
[...]JMenu fileMenu = new JMenu("File");
fileMenu.add(openAction);
[...]JToolBar toolBar = new JToolBar();
toolBar.add(openAction);
:-)class OpenAction extends AbstractAction {
public OpenAction() {
putValue(Action.NAME, "Open");
putValue(Action.SMALL_ICON, new ImageIcon("open16.gif"));
putValue(Action.MNEMONIC_KEY, new Integer('P'));
putValue(Action.SHORT_DESCRIPTION, "open file");
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(
KeyEvent.VK_O, InputEvent.CTRL_MASK));
}
public void actionPerformed(ActionEvent e) {
System.out.println(this);
// ... do something
}
}
I hadn't seen that example before, and I like it quite a lot.
I read this API note at JMenu#add(Action) and JToolBar#add(Action) and it irritated me, because Sun didn't say what is the advantage of their preferred way.
I'd like, however, to point out that the API now says that adding an action directly to a menu, toolbar, etc., is no longer the preferred way (as of 1.3). Instead, it suggests creating the component and using setAction. Of course, the API also shows that common Swing components
now accept an Action in their constructors (also since 1.3). So it's not clear if the preferred way is to use setAction or to instead use something like "new JMenuItem(openAction)". In either case, I'm going to keep this idea in mind for future use.With openAction having a NAME and a SMALL_ICON value,
I found no optical or functional difference between
/*1*/ menu.add(openAction);
and /*2*/ menu.add(new JMenuItem(openAction));
But there is a remarkable optical difference between
/*1*/ toolbar.add(openAction);
and /*2*/ toolbar.add(new JButton(openAction));
(1) gives a tool-button with icon and without text (--> looking fine).
(2) gives a tool-button with icon and with text (--> looking silly).
Therefore, unlike Sun, I still prefer (1).
--
Thomas
.
- References:
- Button event
- From: jaap
- Re: Button event
- From: Thomas Fritsch
- Re: Button event
- From: Steve W. Jackson
- Button event
- Prev by Date: Re: Best Layout?
- Next by Date: JScrollPane or JSplitPane Problem.
- Previous by thread: Re: Button event
- Next by thread: Re: Button event
- Index(es):