views:

222

answers:

1

I'm adding a toolbar to my application and currently I'm adding some toggle buttons to the toolbar. I don't like using the default JButton because it is big and clunky (even if I remove the margins). Are there any libraries for easy creation of toolbars and toolbar buttons that look more native? Particularly, I'd like the buttons to look flat unless the user rolls over them or they are selected (like in Eclipse).

thanks, Jeff

+6  A: 

Instead of adding the JButton directly to the toolbar, create an Action and add the action instead.

The toolbar will create an appropriate JButton that looks correct.

Additionally, set toolbar.setRollover(true), which will make all the buttons flat, except when the mouse is over it.

Oh, and if you don't want it to be floatable, set toolbar.setFloatable(true).

Those three things, and with a correct L&F, the toolbar usually looks very professional

Generally speaking, we shouldn't be mucking around with button margins ourselves unless we're trying to do something extremely custom.

Milan Ramaiya
I guess I didn't try that because the API says that is not the preferred way to do it. If I need toggle buttons though, will I need to write a custom "ActionGroup" (similar to a button group except for actions)?
Jeff Storey
If you using Java 6+ you don't have to write anything custom. Standard action would work. Read about Action's "selected" component property at http://java.sun.com/javase/6/docs/api/javax/swing/Action.html
eugener
True. The part I left out is that I need them to be mutually exclusive. Though that code shouldn't be hard to write. Thanks.
Jeff Storey
JToolbar.add(Action) function returns JButton created by it internally. So it is a matter of adding these buttons to the ButtonGroup.
eugener
For what it is worth, this website says that JToolBar.add(action) is no longer recommended. http://www.dil.univ-mrs.fr/~garreta/docJava/tutorial/uiswing/components/toolbar.html - scroll down to the "The Tool Bar API".
Nemi