wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes How do I avoid icons in menus when using Actions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How do I avoid icons in menus when using Actions" Watch "How do I avoid icons in menus when using Actions" New topic
Author

How do I avoid icons in menus when using Actions

Thomas Nordmeyer
Greenhorn

Joined: Nov 04, 2004
Posts: 2
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

Joined: Apr 28, 2004
Posts: 131
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


Get a better web browser:<br /><a href="http://www.mozilla.org/products/firefox/switch.html" target="_blank" rel="nofollow">http://www.mozilla.org/products/firefox/switch.html</a>
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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


GenRocket - Experts at Building Test Data
Petr Blahos
Ranch Hand

Joined: Apr 28, 2004
Posts: 131
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

Joined: Nov 04, 2004
Posts: 2
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do I avoid icons in menus when using Actions
 
Similar Threads
Polymorphism or conditionals ?
FBNS: Need to provide menu- and toolbar items for all actions ?
Help Info.
Comment on GUI..
Drop Down Tool Bar in a View