• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I avoid icons in menus when using Actions

 
Thomas Nordmeyer
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm using Swing Actions to build both my menu and toolbar. For the Toolbar I define an icon that is properly displayd. Unfortunately the same icon is displayed in the menu, which looks quite ugly. i browsed the JMenuBar API to find a swich without success.

can anybody tell me, how to avoid the action's icon to be displayed in the menu? I would prefere a generic way so this would work for all my actions/menues at once.

thanks,
thomas
 
Petr Blahos
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Thomas,

I did something similar, but I had a different icon for the toolbar then for the menu. I extended AbstractAction so that it called the parent constructor and saved the second (small) icon. Then I inherited my actions from this "AbstractAction2Icons" class.

Apart from that I created my own SmallIconMenuItem (extending JMenuItem). It's constructor tool AbstractAction2Icons as an argument, called super(Action ) and then setIcon to the other icon.

I believe you might be able to call setIcon (null) or something like that. You actually don't need to extend AbstractAction. Just extend JMenuItem, create the constructor that takes Action, call super and then set icon...

Let me know how it went...
Petr
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you doing..

MyAction action = new MyAction();
toolBarButton = new JButton(action);
menuItem = new JMenuItem(action);

or

toolBarButton = new JButton(new MyAction());
menuItem = new JMenuItem(new MyAction());

It would be better to do it the latter. Then, as Petr suggested, pass each instance of the action whatever Icon you want. Take a look at my weblog entry here
 
Petr Blahos
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gregg Bolinger:

It would be better to do it the latter. ...

Actually, what I suggested includes just one instance of action
for both the menu and the toolbar (and a context menu and anything
else...).

Petr
 
Thomas Nordmeyer
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
certainly i'm using the same instance of my action class for both, the menu and the button. otherwise i couldn't share the enabled state.

i find it somehow unsatisfactory that swing seems not to support a central configuration of menus or toolbar buttons. the toolbar icons look apparently ugly in the menus. allways.

i think i will go on like petr suggested ad write my own JMenu abstraction supressing the icon.

thanks for your hints.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic