|
| 1 | +/* |
| 2 | + * Copyright (c) 1999, 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 | +/* @test |
| 25 | + * @bug 4212464 |
| 26 | + * @library /java/awt/regtesthelpers |
| 27 | + * @build PassFailJFrame |
| 28 | + * @summary Verify popup menu borders are drawn correctly when switching L&Fs |
| 29 | + * @run main/manual bug4212464 |
| 30 | + */ |
| 31 | + |
| 32 | +import java.awt.BorderLayout; |
| 33 | +import java.awt.FlowLayout; |
| 34 | +import java.awt.Font; |
| 35 | +import java.awt.event.ActionEvent; |
| 36 | +import java.awt.event.ActionListener; |
| 37 | +import java.awt.event.MouseAdapter; |
| 38 | +import java.awt.event.MouseEvent; |
| 39 | +import javax.swing.JApplet; |
| 40 | +import javax.swing.JButton; |
| 41 | +import javax.swing.JFrame; |
| 42 | +import javax.swing.JLabel; |
| 43 | +import javax.swing.JPanel; |
| 44 | +import javax.swing.JPopupMenu; |
| 45 | +import javax.swing.SwingUtilities; |
| 46 | +import javax.swing.UIManager; |
| 47 | + |
| 48 | +public class bug4212464 extends JFrame implements ActionListener { |
| 49 | + |
| 50 | + static String strMotif = "Motif"; |
| 51 | + static String motifClassName = "com.sun.java.swing.plaf.motif.MotifLookAndFeel"; |
| 52 | + |
| 53 | + static String strMetal = "Metal"; |
| 54 | + static String metalClassName = "javax.swing.plaf.metal.MetalLookAndFeel"; |
| 55 | + |
| 56 | + static bug4212464 frame; |
| 57 | + static JPopupMenu popup; |
| 58 | + |
| 59 | + static final String INSTRUCTIONS = """ |
| 60 | + This test is to see whether popup menu borders behave properly when switching |
| 61 | + back and forth between Motif and Metal L&F. The initial L&F is Metal. |
| 62 | +
|
| 63 | + Pressing the mouse button on the label in the center of the test window brings |
| 64 | + up a popup menu. |
| 65 | +
|
| 66 | + In order to test, use the labeled buttons to switch the look and feel. |
| 67 | + Clicking a button will cause the menu to be hidden. This is OK. Just click the label again. |
| 68 | + Switch back and forth and verify that the popup menu border changes consistently |
| 69 | + and there is a title for the menu when using Motif L&F (Metal won't have a title). |
| 70 | +
|
| 71 | + Make sure you switch back and forth several times. |
| 72 | + If the change is consistent, press PASS otherwise press FAIL. |
| 73 | + """; |
| 74 | + |
| 75 | + public static void main(String[] args) throws Exception { |
| 76 | + PassFailJFrame.builder() |
| 77 | + .instructions(INSTRUCTIONS) |
| 78 | + .columns(50) |
| 79 | + .testUI(bug4212464::createUI) |
| 80 | + .build() |
| 81 | + .awaitAndCheck(); |
| 82 | + } |
| 83 | + |
| 84 | + static JFrame createUI() { |
| 85 | + try { |
| 86 | + UIManager.setLookAndFeel(metalClassName); // initialize to Metal. |
| 87 | + } catch (Exception e) { |
| 88 | + throw new RuntimeException(e); |
| 89 | + } |
| 90 | + frame = new bug4212464("bug4212464"); |
| 91 | + popup = new JPopupMenu("Test"); |
| 92 | + popup.add("Item 1"); |
| 93 | + popup.add("Item 2"); |
| 94 | + popup.add("Item 3"); |
| 95 | + popup.add("Item 4"); |
| 96 | + |
| 97 | + JPanel p = new JPanel(); |
| 98 | + p.setLayout(new FlowLayout()); |
| 99 | + JButton motif = (JButton)p.add(new JButton(strMotif)); |
| 100 | + JButton metal = (JButton)p.add(new JButton(strMetal)); |
| 101 | + motif.setActionCommand(motifClassName); |
| 102 | + metal.setActionCommand(metalClassName); |
| 103 | + motif.addActionListener(frame); |
| 104 | + metal.addActionListener(frame); |
| 105 | + frame.add(BorderLayout.NORTH, p); |
| 106 | + |
| 107 | + JLabel l = new JLabel("Click any mouse button on this big label"); |
| 108 | + l.setFont(new Font(Font.DIALOG, Font.PLAIN, 20)); |
| 109 | + l.addMouseListener(new MouseAdapter() { |
| 110 | + public void mousePressed(MouseEvent e) { |
| 111 | + popup.show(e.getComponent(), e.getX(), e.getY()); |
| 112 | + } |
| 113 | + }); |
| 114 | + frame.add(BorderLayout.CENTER, l); |
| 115 | + frame.setSize(500, 400); |
| 116 | + return frame; |
| 117 | + } |
| 118 | + |
| 119 | + public bug4212464(String title) { |
| 120 | + super(title); |
| 121 | + } |
| 122 | + |
| 123 | + public void actionPerformed(ActionEvent e) { |
| 124 | + String str = e.getActionCommand(); |
| 125 | + if (str.equals(metalClassName) || str.equals(motifClassName)) { |
| 126 | + changeLNF(str); |
| 127 | + } else { |
| 128 | + System.out.println("ActionEvent: " + str); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + public void changeLNF(String str) { |
| 133 | + System.out.println("Changing LNF to " + str); |
| 134 | + try { |
| 135 | + UIManager.setLookAndFeel(str); |
| 136 | + SwingUtilities.updateComponentTreeUI(frame); |
| 137 | + SwingUtilities.updateComponentTreeUI(popup); |
| 138 | + } catch (Exception e) { |
| 139 | + throw new RuntimeException(e); |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments