提交 48626e23 编写于 作者: A alexp

6739756: JToolBar leaves space for non-visible items under Nimbus L&F

Reviewed-by: peterz
上级 9dcc82ce
...@@ -363,18 +363,24 @@ public class SynthToolBarUI extends BasicToolBarUI ...@@ -363,18 +363,24 @@ public class SynthToolBarUI extends BasicToolBarUI
SynthIcon.getIconWidth(handleIcon, context) : 0; SynthIcon.getIconWidth(handleIcon, context) : 0;
Dimension compDim; Dimension compDim;
for (int i = 0; i < tb.getComponentCount(); i++) { for (int i = 0; i < tb.getComponentCount(); i++) {
compDim = tb.getComponent(i).getMinimumSize(); Component component = tb.getComponent(i);
dim.width += compDim.width; if (component.isVisible()) {
dim.height = Math.max(dim.height, compDim.height); compDim = component.getMinimumSize();
dim.width += compDim.width;
dim.height = Math.max(dim.height, compDim.height);
}
} }
} else { } else {
dim.height = tb.isFloatable() ? dim.height = tb.isFloatable() ?
SynthIcon.getIconHeight(handleIcon, context) : 0; SynthIcon.getIconHeight(handleIcon, context) : 0;
Dimension compDim; Dimension compDim;
for (int i = 0; i < tb.getComponentCount(); i++) { for (int i = 0; i < tb.getComponentCount(); i++) {
compDim = tb.getComponent(i).getMinimumSize(); Component component = tb.getComponent(i);
dim.width = Math.max(dim.width, compDim.width); if (component.isVisible()) {
dim.height += compDim.height; compDim = component.getMinimumSize();
dim.width = Math.max(dim.width, compDim.width);
dim.height += compDim.height;
}
} }
} }
dim.width += insets.left + insets.right; dim.width += insets.left + insets.right;
...@@ -395,18 +401,24 @@ public class SynthToolBarUI extends BasicToolBarUI ...@@ -395,18 +401,24 @@ public class SynthToolBarUI extends BasicToolBarUI
SynthIcon.getIconWidth(handleIcon, context) : 0; SynthIcon.getIconWidth(handleIcon, context) : 0;
Dimension compDim; Dimension compDim;
for (int i = 0; i < tb.getComponentCount(); i++) { for (int i = 0; i < tb.getComponentCount(); i++) {
compDim = tb.getComponent(i).getPreferredSize(); Component component = tb.getComponent(i);
dim.width += compDim.width; if (component.isVisible()) {
dim.height = Math.max(dim.height, compDim.height); compDim = component.getPreferredSize();
dim.width += compDim.width;
dim.height = Math.max(dim.height, compDim.height);
}
} }
} else { } else {
dim.height = tb.isFloatable() ? dim.height = tb.isFloatable() ?
SynthIcon.getIconHeight(handleIcon, context) : 0; SynthIcon.getIconHeight(handleIcon, context) : 0;
Dimension compDim; Dimension compDim;
for (int i = 0; i < tb.getComponentCount(); i++) { for (int i = 0; i < tb.getComponentCount(); i++) {
compDim = tb.getComponent(i).getPreferredSize(); Component component = tb.getComponent(i);
dim.width = Math.max(dim.width, compDim.width); if (component.isVisible()) {
dim.height += compDim.height; compDim = component.getPreferredSize();
dim.width = Math.max(dim.width, compDim.width);
dim.height += compDim.height;
}
} }
} }
dim.width += insets.left + insets.right; dim.width += insets.left + insets.right;
...@@ -469,22 +481,24 @@ public class SynthToolBarUI extends BasicToolBarUI ...@@ -469,22 +481,24 @@ public class SynthToolBarUI extends BasicToolBarUI
for (int i = 0; i < tb.getComponentCount(); i++) { for (int i = 0; i < tb.getComponentCount(); i++) {
c = tb.getComponent(i); c = tb.getComponent(i);
d = c.getPreferredSize(); if (c.isVisible()) {
int y, h; d = c.getPreferredSize();
if (d.height >= baseH || c instanceof JSeparator) { int y, h;
// Fill available height if (d.height >= baseH || c instanceof JSeparator) {
y = baseY; // Fill available height
h = baseH; y = baseY;
} else { h = baseH;
// Center component vertically in the available space } else {
y = baseY + (baseH / 2) - (d.height / 2); // Center component vertically in the available space
h = d.height; y = baseY + (baseH / 2) - (d.height / 2);
h = d.height;
}
//if the component is a "glue" component then add to its
//width the extraSpacePerGlue it is due
if (isGlue(c)) d.width += extraSpacePerGlue;
c.setBounds(ltr ? x : x - d.width, y, d.width, h);
x = ltr ? x + d.width : x - d.width;
} }
//if the component is a "glue" component then add to its
//width the extraSpacePerGlue it is due
if (isGlue(c)) d.width += extraSpacePerGlue;
c.setBounds(ltr ? x : x - d.width, y, d.width, h);
x = ltr ? x + d.width : x - d.width;
} }
} else { } else {
int handleHeight = tb.isFloatable() ? int handleHeight = tb.isFloatable() ?
...@@ -512,29 +526,31 @@ public class SynthToolBarUI extends BasicToolBarUI ...@@ -512,29 +526,31 @@ public class SynthToolBarUI extends BasicToolBarUI
for (int i = 0; i < tb.getComponentCount(); i++) { for (int i = 0; i < tb.getComponentCount(); i++) {
c = tb.getComponent(i); c = tb.getComponent(i);
d = c.getPreferredSize(); if (c.isVisible()) {
int x, w; d = c.getPreferredSize();
if (d.width >= baseW || c instanceof JSeparator) { int x, w;
// Fill available width if (d.width >= baseW || c instanceof JSeparator) {
x = baseX; // Fill available width
w = baseW; x = baseX;
} else { w = baseW;
// Center component horizontally in the available space } else {
x = baseX + (baseW / 2) - (d.width / 2); // Center component horizontally in the available space
w = d.width; x = baseX + (baseW / 2) - (d.width / 2);
w = d.width;
}
//if the component is a "glue" component then add to its
//height the extraSpacePerGlue it is due
if (isGlue(c)) d.height += extraSpacePerGlue;
c.setBounds(x, y, w, d.height);
y += d.height;
} }
//if the component is a "glue" component then add to its
//height the extraSpacePerGlue it is due
if (isGlue(c)) d.height += extraSpacePerGlue;
c.setBounds(x, y, w, d.height);
y += d.height;
} }
} }
context.dispose(); context.dispose();
} }
private boolean isGlue(Component c) { private boolean isGlue(Component c) {
if (c instanceof Box.Filler) { if (c.isVisible() && c instanceof Box.Filler) {
Box.Filler f = (Box.Filler)c; Box.Filler f = (Box.Filler)c;
Dimension min = f.getMinimumSize(); Dimension min = f.getMinimumSize();
Dimension pref = f.getPreferredSize(); Dimension pref = f.getPreferredSize();
......
/*
* Copyright 2010 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/**
* @test
* @bug 6739756
* @author Alexander Potochkin
* @summary JToolBar leaves space for non-visible items under Nimbus L&F
* @run main bug6739756
*/
import javax.swing.*;
import java.awt.*;
public class bug6739756 {
public static void main(String[] args) throws Exception {
try {
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
}
catch (Exception e) {
e.printStackTrace();
return;
}
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
JToolBar tb = new JToolBar();
Dimension preferredSize = tb.getPreferredSize();
JButton button = new JButton("Test");
button.setVisible(false);
tb.add(button);
if (!preferredSize.equals(tb.getPreferredSize())) {
throw new RuntimeException("Toolbar's preferredSize is wrong");
}
}
});
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册