|
| 1 | +/* |
| 2 | + * Copyright (c) 2002, 2022, 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 | +/* @test |
| 24 | + * @bug 4380543 |
| 25 | + * @key headful |
| 26 | + * @library /java/awt/regtesthelpers |
| 27 | + * @build PassFailJFrame |
| 28 | + * @summary setMargin() does not work for AbstractButton |
| 29 | + * @run main/manual bug4380543 |
| 30 | +*/ |
| 31 | + |
| 32 | +import java.awt.BorderLayout; |
| 33 | +import java.awt.Color; |
| 34 | +import java.awt.event.ActionEvent; |
| 35 | +import java.awt.event.ActionListener; |
| 36 | +import java.awt.Insets; |
| 37 | + |
| 38 | +import javax.swing.BoxLayout; |
| 39 | +import javax.swing.JButton; |
| 40 | +import javax.swing.JCheckBox; |
| 41 | +import javax.swing.JFrame; |
| 42 | +import javax.swing.JPanel; |
| 43 | +import javax.swing.JRadioButton; |
| 44 | +import javax.swing.SwingUtilities; |
| 45 | +import javax.swing.UIManager; |
| 46 | +import javax.swing.UnsupportedLookAndFeelException; |
| 47 | + |
| 48 | +public class bug4380543 { |
| 49 | + static TestFrame testObj; |
| 50 | + static String instructions |
| 51 | + = |
| 52 | + "INSTRUCTIONS:\n" + |
| 53 | + " 1. Check if the Left inset(margin) is set visually\n" + |
| 54 | + " similar to other three sides around the Radio Button\n" + |
| 55 | + " and CheckBox (insets set to 20 on all 4 sides).\n" + |
| 56 | + " 2. Rendering depends on OS and supported Look and Feels.\n" + |
| 57 | + " Verify only with those L&F where margins are visible.\n" + |
| 58 | + " 3. If the Left inset(margin) appears too small, press Fail,\n" + |
| 59 | + " else press Pass." |
| 60 | + ; |
| 61 | + static PassFailJFrame passFailJFrame; |
| 62 | + |
| 63 | + public static void main(String[] args) throws Exception { |
| 64 | + |
| 65 | + SwingUtilities.invokeAndWait(new Runnable() { |
| 66 | + public void run() { |
| 67 | + try { |
| 68 | + passFailJFrame = new PassFailJFrame(instructions); |
| 69 | + testObj = new TestFrame(); |
| 70 | + //Adding the Test Frame to handle dispose |
| 71 | + PassFailJFrame.addTestFrame(testObj); |
| 72 | + PassFailJFrame.positionTestFrame(testObj, PassFailJFrame.Position.HORIZONTAL); |
| 73 | + } catch (Exception e) { |
| 74 | + e.printStackTrace(); |
| 75 | + } |
| 76 | + } |
| 77 | + }); |
| 78 | + passFailJFrame.awaitAndCheck(); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +class TestFrame extends JFrame implements ActionListener { |
| 83 | + public TestFrame() { |
| 84 | + initComponents(); |
| 85 | + } |
| 86 | + |
| 87 | + public void initComponents() { |
| 88 | + JPanel p = new JPanel(); |
| 89 | + JPanel buttonsPanel = new JPanel(); |
| 90 | + buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.Y_AXIS)); |
| 91 | + |
| 92 | + JRadioButton rb = new JRadioButton("JRadioButton"); |
| 93 | + rb.setMargin(new Insets(20, 20, 20, 20)); |
| 94 | + rb.setBackground(Color.GREEN); |
| 95 | + rb.setAlignmentX(0.5f); |
| 96 | + buttonsPanel.add(rb); |
| 97 | + |
| 98 | + JCheckBox cb = new JCheckBox("JCheckBox"); |
| 99 | + cb.setMargin(new Insets(20, 20, 20, 20)); |
| 100 | + cb.setBackground(Color.YELLOW); |
| 101 | + cb.setAlignmentX(0.5f); |
| 102 | + buttonsPanel.add(cb); |
| 103 | + |
| 104 | + getContentPane().add(buttonsPanel); |
| 105 | + UIManager.LookAndFeelInfo[] lookAndFeel = UIManager.getInstalledLookAndFeels(); |
| 106 | + for (UIManager.LookAndFeelInfo look : lookAndFeel) { |
| 107 | + JButton btn = new JButton(look.getName()); |
| 108 | + btn.setActionCommand(look.getClassName()); |
| 109 | + btn.addActionListener(this); |
| 110 | + p.add(btn); |
| 111 | + } |
| 112 | + |
| 113 | + getContentPane().add(p,BorderLayout.SOUTH); |
| 114 | + |
| 115 | + setSize(500, 300); |
| 116 | + setVisible(true); |
| 117 | + } |
| 118 | + |
| 119 | + private static void setLookAndFeel(String laf) { |
| 120 | + try { |
| 121 | + UIManager.setLookAndFeel(laf); |
| 122 | + } catch (UnsupportedLookAndFeelException ignored) { |
| 123 | + System.out.println("Unsupported L&F: " + laf); |
| 124 | + } catch (ClassNotFoundException | InstantiationException |
| 125 | + | IllegalAccessException e) { |
| 126 | + throw new RuntimeException(e); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + //Changing the Look and Feel on user selection |
| 131 | + public void actionPerformed(ActionEvent e) { |
| 132 | + setLookAndFeel(e.getActionCommand()); |
| 133 | + SwingUtilities.updateComponentTreeUI(this); |
| 134 | + } |
| 135 | +} |
0 commit comments