|
| 1 | +/* |
| 2 | + * Copyright (c) 2001, 2025, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +/* |
| 25 | + * @test |
| 26 | + * @bug 4275631 |
| 27 | + * @summary Tests if vertical JSlider is properly aligned in large container |
| 28 | + * @key headful |
| 29 | + * @run main bug4275631 |
| 30 | + */ |
| 31 | + |
| 32 | +import java.awt.BorderLayout; |
| 33 | +import java.awt.Dimension; |
| 34 | +import java.awt.GridBagConstraints; |
| 35 | +import java.awt.GridBagLayout; |
| 36 | +import java.awt.Point; |
| 37 | +import java.awt.Robot; |
| 38 | + |
| 39 | +import javax.swing.BorderFactory; |
| 40 | +import javax.swing.Box; |
| 41 | +import javax.swing.JFrame; |
| 42 | +import javax.swing.JPanel; |
| 43 | +import javax.swing.JSlider; |
| 44 | +import javax.swing.SwingUtilities; |
| 45 | + |
| 46 | +public class bug4275631 { |
| 47 | + private static final int OFFSET = 1; |
| 48 | + private static JFrame f; |
| 49 | + private static JSlider slider1; |
| 50 | + private static JSlider slider2; |
| 51 | + private static volatile Point loc1; |
| 52 | + private static volatile Point loc2; |
| 53 | + |
| 54 | + public static void main(String[] args) throws Exception { |
| 55 | + try { |
| 56 | + SwingUtilities.invokeAndWait(() -> { |
| 57 | + f = new JFrame("JSlider Alignment Test"); |
| 58 | + f.setSize(400, 200); |
| 59 | + f.setLocationRelativeTo(null); |
| 60 | + |
| 61 | + // Create two sliders, verify the alignment on the slider to be |
| 62 | + // used in the border layout |
| 63 | + slider1 = new JSlider(JSlider.VERTICAL, 0, 99, 50); |
| 64 | + slider1.setInverted(true); |
| 65 | + slider1.setMajorTickSpacing(10); |
| 66 | + slider1.setMinorTickSpacing(1); |
| 67 | + slider1.setPaintTicks(true); |
| 68 | + slider1.setPaintLabels(true); |
| 69 | + slider2 = new JSlider(JSlider.VERTICAL, 0, 99, 50); |
| 70 | + slider2.setInverted(true); |
| 71 | + slider2.setMajorTickSpacing(10); |
| 72 | + slider2.setMinorTickSpacing(1); |
| 73 | + slider2.setPaintTicks(true); |
| 74 | + slider2.setPaintLabels(true); |
| 75 | + |
| 76 | + // Try to center the natural way, using a border layout in the "Center" |
| 77 | + JPanel borderPanel = new JPanel(); |
| 78 | + borderPanel.setLayout(new BorderLayout()); |
| 79 | + borderPanel.setBorder(BorderFactory.createTitledBorder("BorderLayout")); |
| 80 | + borderPanel.add(slider1, BorderLayout.CENTER); |
| 81 | + borderPanel.setPreferredSize(new Dimension(200, 200)); |
| 82 | + |
| 83 | + // Try to center using GridBagLayout, with glue on left |
| 84 | + // and right to squeeze slider into place |
| 85 | + JPanel gridBagPanel = new JPanel(new GridBagLayout()); |
| 86 | + gridBagPanel.setBorder(BorderFactory.createTitledBorder("GridBagLayout")); |
| 87 | + GridBagConstraints c = new GridBagConstraints(); |
| 88 | + c.gridx = 1; |
| 89 | + c.fill = GridBagConstraints.VERTICAL; |
| 90 | + c.weighty = 1.0; |
| 91 | + gridBagPanel.add(slider2, c); |
| 92 | + c.gridx = 0; |
| 93 | + c.fill = GridBagConstraints.BOTH; |
| 94 | + c.weighty = 0.0; |
| 95 | + gridBagPanel.add(Box.createHorizontalGlue(), c); |
| 96 | + c.gridx = 2; |
| 97 | + c.fill = GridBagConstraints.BOTH; |
| 98 | + gridBagPanel.add(Box.createHorizontalGlue(), c); |
| 99 | + gridBagPanel.setPreferredSize(new Dimension(200, 200)); |
| 100 | + |
| 101 | + f.add(borderPanel, BorderLayout.WEST); |
| 102 | + f.add(gridBagPanel, BorderLayout.EAST); |
| 103 | + f.setVisible(true); |
| 104 | + }); |
| 105 | + |
| 106 | + Robot r = new Robot(); |
| 107 | + r.setAutoDelay(100); |
| 108 | + r.waitForIdle(); |
| 109 | + r.delay(1000); |
| 110 | + |
| 111 | + SwingUtilities.invokeAndWait(() -> { |
| 112 | + loc1 = slider1.getLocationOnScreen(); |
| 113 | + loc1.setLocation(loc1.x + (slider1.getWidth() / 2), |
| 114 | + loc1.y + (slider1.getHeight() / 2)); |
| 115 | + |
| 116 | + loc2 = slider2.getLocationOnScreen(); |
| 117 | + loc2.setLocation(loc2.x + (slider2.getWidth() / 2), |
| 118 | + loc2.y + (slider2.getHeight() / 2)); |
| 119 | + }); |
| 120 | + |
| 121 | + if (loc1.y > loc2.y + OFFSET || loc1.y < loc2.y - OFFSET) { |
| 122 | + throw new RuntimeException("JSlider position is not aligned!"); |
| 123 | + } |
| 124 | + } finally { |
| 125 | + SwingUtilities.invokeAndWait(() -> { |
| 126 | + if (f != null) { |
| 127 | + f.dispose(); |
| 128 | + } |
| 129 | + }); |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments