When you add a JButton to a JToolbar, the button takes a specific look (not the same that if you add it to a Jpanel). I created some component similar to a JToolbar and I would like the same behavior. Problem: I checked the JToolbar class to find some specific code in charge to change the appearence of added components (change component specific paint method or UI delegates, etc). I didn't find anything! I don't understand how JToolbar works. Could anyone explain me how it works?
Thanks a lot,
Herve Guillaume
This appears to be handled by the update()
method of the MetalButtonUI
class. Here is the code from JDK5_07:
public void update(Graphics g, JComponent c) {
AbstractButton button = (AbstractButton)c;
if ((c.getBackground() instanceof UIResource) &&
button.isContentAreaFilled() && c.isEnabled()) {
ButtonModel model = button.getModel();
if (!MetalUtils.isToolBarButton(c)) {
if (!model.isArmed() && !model.isPressed() &&
MetalUtils.drawGradient(
c, g, "Button.gradient", 0, 0, c.getWidth(),
c.getHeight(), true)) {
paint(g, c);
return;
}
}
else if (model.isRollover() && MetalUtils.drawGradient(
c, g, "Button.gradient", 0, 0, c.getWidth(),
c.getHeight(), true)) {
paint(g, c);
return;
}
}
super.update(g, c);
}
The isToolBarButton()
method just checks if the parent Container is a JToolBar, so I guess one solution is to always add your JButton to a JToolBar and then add the toolbar to your real Container.
Otherwise, I guess, you would need to write your own custom UI and override the update() method.