Hi, this is the first fix to make painting of JButtons which are direct childs of a JToolBar correct.
I found out that those button will not draw their extra thick border when the mouse is hovering above them. I fixed that in the MetalBorder.ButtonBorder class. Additionally I found out that the RI uses this class to replace the Buttons border when adding them to the JToolBar. I changed BasicToolBarUI to this. Ah yes: The how the classes decide whether they paint toolbar-style or not is pretty dumb but I found no other hint than that. There is no unique property on the button or their model which changes when the button is added to the toolbar. Another clue that this is the way it is done in the RI is that if you put your button into a panel and add that panel into a toolbar the button is painted in the normal way ... Another patch will deal with the buttons own painting. The ChangeLog: 2006-05-18 Robert Schuster <[EMAIL PROTECTED]> * javax/swing/plaf/basic/BasicToolBarUI.java: (navigateFocusedComp): Marked as stub. (createRolloverBorder): Create a different Border instance, added note. * javax/swing/plaf/metal/MetalBorders.java: (ButtonBorder): Added documentation. (ButtonBorder.paintDefaultButtonBorder): Added else-block. (ButtonBorder.paintOceanButtonBorder): Added else-block, added subexpression into if-else cascade, added note. cya Robert
Index: javax/swing/plaf/basic/BasicToolBarUI.java =================================================================== RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicToolBarUI.java,v retrieving revision 1.22 diff -u -r1.22 BasicToolBarUI.java --- javax/swing/plaf/basic/BasicToolBarUI.java 17 Apr 2006 07:41:05 -0000 1.22 +++ javax/swing/plaf/basic/BasicToolBarUI.java 18 May 2006 17:01:36 -0000 @@ -75,11 +75,14 @@ import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.border.Border; +import javax.swing.border.CompoundBorder; +import javax.swing.border.EmptyBorder; import javax.swing.border.EtchedBorder; import javax.swing.event.MouseInputListener; import javax.swing.plaf.ComponentUI; import javax.swing.plaf.ToolBarUI; import javax.swing.plaf.UIResource; +import javax.swing.plaf.metal.MetalBorders; /** * This is the Basic Look and Feel UI class for JToolBar. @@ -331,18 +334,12 @@ */ protected Border createRolloverBorder() { - return new EtchedBorder() - { - public void paintBorder(Component c, Graphics g, int x, int y, - int width, int height) - { - if (c instanceof JButton) - { - if (((JButton) c).getModel().isRollover()) - super.paintBorder(c, g, x, y, width, height); - } - } - }; + // We can safely assume that the component on which this border + // will be installed is a AbstractButton (or subclass). So + // a MetalBorders.ButtonBorder will work on it. + return new CompoundBorder( + new MetalBorders.ButtonBorder(), + new EmptyBorder(3,3,3,3)); } /** @@ -745,6 +742,7 @@ * @param direction The direction to give focus to. */ protected void navigateFocusedComp(int direction) + throws NotImplementedException { // FIXME: Implement. } Index: javax/swing/plaf/metal/MetalBorders.java =================================================================== RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/metal/MetalBorders.java,v retrieving revision 1.34 diff -u -r1.34 MetalBorders.java --- javax/swing/plaf/metal/MetalBorders.java 11 May 2006 17:05:55 -0000 1.34 +++ javax/swing/plaf/metal/MetalBorders.java 18 May 2006 17:01:36 -0000 @@ -103,7 +103,16 @@ private static BasicBorders.MarginBorder marginBorder; /** - * A border used for [EMAIL PROTECTED] JButton} components. + * <p>A border used for [EMAIL PROTECTED] JButton} components.</p> + * + * <p>This [EMAIL PROTECTED] Border} implementation can handle only instances of + * [EMAIL PROTECTED] AbstractButton} and their subclasses.</p> + * + * <p>If the Metal Look and Feel's current theme is 'Ocean' the border + * will be painted with a special highlight when the mouse cursor if + * over the button (ie. the property <code>rollover</code> of the + * button's model is <code>true</code>) and is not a <b>direct</b> + * child of a [EMAIL PROTECTED] JToolBar}.</p> */ public static class ButtonBorder extends AbstractBorder implements UIResource { @@ -157,8 +166,14 @@ { ButtonModel bmodel = null; + // The RI will fail with a ClassCastException in such a situation. + // This code tries to be more helpful. if (c instanceof AbstractButton) bmodel = ((AbstractButton) c).getModel(); + else + throw new IllegalStateException("A ButtonBorder is supposed to work " + + "only with AbstractButton and" + + "subclasses."); Color darkShadow = MetalLookAndFeel.getControlDarkShadow(); Color shadow = MetalLookAndFeel.getControlShadow(); @@ -246,8 +261,14 @@ { ButtonModel bmodel = null; + // The RI will fail with a ClassCastException in such a situation. + // This code tries to be more helpful. if (c instanceof AbstractButton) bmodel = ((AbstractButton) c).getModel(); + else + throw new IllegalStateException("A ButtonBorder is supposed to work " + + "only with AbstractButton and" + + "subclasses."); Color darkShadow = MetalLookAndFeel.getControlDarkShadow(); Color shadow = MetalLookAndFeel.getControlShadow(); @@ -267,8 +288,10 @@ g.drawRect(x, y, w - 1, h - 1); g.drawRect(x + 1, y + 1, w - 3, h - 3); } - else if (bmodel.isRollover()) + else if (bmodel.isRollover() && !(c.getParent() instanceof JToolBar)) { + // Paint a bigger border when the mouse is over the button but + // only if it is *not* part of a JToolBar. g.setColor(shadow); g.drawRect(x, y, w - 1, h - 1); g.drawRect(x + 2, y + 2, w - 5, h - 5);
signature.asc
Description: OpenPGP digital signature