Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4346610: Adding JSeparator to JToolBar "pushes" buttons added after separator to edge #15054

Closed
wants to merge 10 commits into from
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. 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
@@ -25,12 +25,14 @@

package javax.swing.plaf.basic;

import javax.swing.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Rectangle;
import javax.swing.JComponent;
import javax.swing.JSeparator;
import javax.swing.LookAndFeel;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.SeparatorUI;

@@ -152,13 +154,13 @@ public Dimension getPreferredSize( JComponent c )
}

public Dimension getMinimumSize( JComponent c ) { return null; }

public Dimension getMaximumSize( JComponent c ) {
Dimension d = getPreferredSize(c);
if (((JSeparator)c).getOrientation() == JSeparator.VERTICAL) {
return new Dimension(d.width, Integer.MAX_VALUE);
}
else {
return new Dimension(Integer.MAX_VALUE, d.height);
return new Dimension(d.width, Short.MAX_VALUE);
} else {
return new Dimension(Short.MAX_VALUE, d.height);
}
}
}
36 changes: 13 additions & 23 deletions test/jdk/javax/swing/JToolBar/ToolBarSeparatorTest.java
Original file line number Diff line number Diff line change
@@ -51,11 +51,13 @@
public class ToolBarSeparatorTest {

private static JFrame frame;
private static JSeparator separator;
private static JToolBar toolBar;
private static JButton btn;
private static volatile Point pt;
private static volatile Dimension size;
private static volatile int btnWidth;
private static volatile int sepWidth;
private static volatile int sepPrefWidth;

public static void main(String[] args) throws Exception {
Robot robot = new Robot();
@@ -68,7 +70,8 @@ public static void main(String[] args) throws Exception {
btn = new JButton("button 1");
toolBar.add(btn);
toolBar.add(new JButton("button 2"));
toolBar.add(new JSeparator(SwingConstants.VERTICAL));
separator = new JSeparator(SwingConstants.VERTICAL);
toolBar.add(separator);
toolBar.add(new JButton("button 3"));
toolBar.setBackground(Color.red);
frame.getContentPane().setLayout(new BorderLayout());
@@ -83,29 +86,16 @@ public static void main(String[] args) throws Exception {
SwingUtilities.invokeAndWait(() -> {
pt = toolBar.getLocationOnScreen();
size = toolBar.getSize();
btnWidth = btn.getWidth();
sepWidth = separator.getSize().width;
sepPrefWidth = separator.getPreferredSize().width;
});
boolean passed = true;
System.out.println("point " + pt + " size " + size +
" btn width " + btn.getWidth());

// Capture button width area after 2 buttons which shouldn't be red
BufferedImage img = robot.createScreenCapture(
new Rectangle(pt.x + btnWidth*2 + 20, pt.y, btnWidth, size.height));

int y = img.getHeight() / 2;
for (int x = 10; x < img.getWidth(); x += 10) {
System.out.println("x " + x + " y " + y +
" color: " + new Color(img.getRGB(x, y)));
Color c = new Color(img.getRGB(x, y));
if (c.equals(Color.RED)) {
passed = false;
break;
}
}
if (!passed) {
if (separator.getSize().width != separator.getPreferredSize().width) {
System.out.println("size " + sepWidth);
System.out.println("preferredsize " + sepPrefWidth);
BufferedImage img = robot.createScreenCapture(
new Rectangle(pt.x, pt.y, size.width, size.height));
ImageIO.write(img, "png", new java.io.File("image.png"));
throw new RuntimeException("Separator takes more space");
throw new RuntimeException("separator size is too wide");
}
} finally {
SwingUtilities.invokeAndWait(() -> {