11/*
2- * Copyright (c) 2013, 2018 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2013, 2025 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
2626 * @bug 4796987
2727 * @key headful
2828 * @requires (os.family == "windows")
29- * @summary XP Only: JButton.setBorderPainted() does not work with XP L&F
30- * @author Alexander Scherbatiy
29+ * @summary Verify JButton.setBorderPainted(false) removes border
30+ * for Windows visual styles (Windows XP and later)
3131 * @library ../../regtesthelpers
32- * @library /test/lib
33- * @modules java.desktop/com.sun.java.swing.plaf.windows
34- * java.desktop/sun.awt
35- * @build jdk.test.lib.OSVersion jdk.test.lib.Platform
3632 * @build Util
3733 * @run main bug4796987
3834 */
3935
40- import jdk .test .lib .Platform ;
41- import jdk .test .lib .OSVersion ;
42- import java .awt .*;
43- import javax .swing .*;
44- import com .sun .java .swing .plaf .windows .WindowsLookAndFeel ;
36+ import java .awt .BorderLayout ;
37+ import java .awt .Dimension ;
38+ import java .awt .Point ;
39+ import java .awt .Rectangle ;
40+ import java .awt .Robot ;
41+ import java .awt .image .BufferedImage ;
42+ import java .io .File ;
43+ import java .io .IOException ;
44+
45+ import javax .imageio .ImageIO ;
46+ import javax .swing .JButton ;
47+ import javax .swing .JFrame ;
48+ import javax .swing .JPanel ;
49+ import javax .swing .SwingUtilities ;
50+ import javax .swing .UIManager ;
4551
4652public class bug4796987 {
4753
4854 private static JButton button1 ;
4955 private static JButton button2 ;
5056 private static JFrame frame ;
57+ private static JPanel panel ;
5158
5259 public static void main (String [] args ) throws Exception {
5360 try {
54- if (Platform .isWindows ()
55- && OSVersion .current ().equals (OSVersion .WINDOWS_XP )) {
56- UIManager .setLookAndFeel (new WindowsLookAndFeel ());
57- testButtonBorder ();
58- }
61+ UIManager .setLookAndFeel (UIManager .getSystemLookAndFeelClassName ());
62+ testButtonBorder ();
5963 } finally {
60- if (frame != null ) SwingUtilities .invokeAndWait (() -> frame .dispose ());
64+ SwingUtilities .invokeAndWait (() -> {
65+ if (frame != null ) {
66+ frame .dispose ();
67+ }
68+ });
6169 }
6270 }
6371
6472 private static void testButtonBorder () throws Exception {
6573 Robot robot = new Robot ();
66- robot .setAutoDelay (50 );
6774
68- SwingUtilities .invokeAndWait (new Runnable () {
69-
70- public void run () {
71- createAndShowGUI ();
72- }
73- });
75+ SwingUtilities .invokeAndWait (bug4796987 ::createAndShowGUI );
76+ robot .waitForIdle ();
77+ robot .delay (500 );
7478
79+ // Hover over button1
80+ Point b1Center = Util .getCenterPoint (button1 );
81+ robot .mouseMove (b1Center .x , b1Center .y );
7582 robot .waitForIdle ();
76- Thread .sleep (500 );
7783
78- Point p1 = Util .getCenterPoint (button1 );
79- Point p2 = Util .getCenterPoint (button2 );
84+ Rectangle panelBounds = Util .invokeOnEDT (() ->
85+ new Rectangle (panel .getLocationOnScreen (),
86+ panel .getSize ()));
87+ BufferedImage image = robot .createScreenCapture (panelBounds );
88+
89+ final Point p1 = Util .invokeOnEDT (() -> getCenterPoint (button1 ));
90+ final Point p2 = Util .invokeOnEDT (() -> getCenterPoint (button2 ));
8091
81- Color color = robot .getPixelColor (p1 .x , p2 .x );
82- for (int dx = p1 .x ; dx < p2 .x - p1 .x ; dx ++) {
83- robot .mouseMove (p1 .x + dx , p1 .y );
84- if (!color .equals (robot .getPixelColor (p1 .x + dx , p1 .y ))) {
92+ final int color = image .getRGB (p1 .x , p1 .y );
93+ for (int dx = 0 ; p1 .x + dx < p2 .x ; dx ++) {
94+ if (color != image .getRGB (p1 .x + dx , p1 .y )) {
95+ System .err .println ("Wrong color at " + (p1 .x + dx ) + ", " + p1 .y
96+ + " - expected " + Integer .toHexString (color ));
97+ saveImage (image );
8598 throw new RuntimeException ("Button has border and background!" );
8699 }
87100 }
88101 }
89102
103+ /**
104+ * {@return the center point of a button relative to its parent}
105+ * @param button the button to calculate the center point
106+ */
107+ private static Point getCenterPoint (JButton button ) {
108+ Point location = button .getLocation ();
109+ Dimension size = button .getSize ();
110+ location .translate (size .width / 2 , size .height / 2 );
111+ return location ;
112+ }
113+
90114 private static JButton getButton () {
91115 JButton button = new JButton ();
92116 button .setBorderPainted (false );
@@ -95,18 +119,24 @@ private static JButton getButton() {
95119 }
96120
97121 private static void createAndShowGUI () {
98- frame = new JFrame ("Test " );
122+ frame = new JFrame ("bug4796987 " );
99123 frame .setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE );
100124 frame .setSize (200 , 200 );
101125
102- JButton button = new JButton ();
103- button .setBorder (null );
104-
105- JPanel panel = new JPanel (new BorderLayout (50 , 50 ));
126+ panel = new JPanel (new BorderLayout (50 , 50 ));
106127 panel .add (getButton (), BorderLayout .CENTER );
107128 panel .add (button1 = getButton (), BorderLayout .WEST );
108129 panel .add (button2 = getButton (), BorderLayout .EAST );
109130 frame .getContentPane ().add (panel );
131+ frame .setLocationRelativeTo (null );
110132 frame .setVisible (true );
111133 }
134+
135+ private static void saveImage (BufferedImage image ) {
136+ try {
137+ ImageIO .write (image , "png" ,
138+ new File ("frame.png" ));
139+ } catch (IOException ignored ) {
140+ }
141+ }
112142}
0 commit comments