1 /* 2 * Copyright (c) 2009, 2017, 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 8178430 26 * @summary JMenu in GridBagLayout flickers when label text shows "..." and 27 * is updated 28 * @run main LabelDotTest 29 */ 30 import java.awt.Dimension; 31 import java.awt.GridBagConstraints; 32 import java.awt.GridBagLayout; 33 import java.awt.Robot; 34 35 import java.util.stream.IntStream; 36 37 import javax.swing.SwingUtilities; 38 import javax.swing.JLabel; 39 import javax.swing.JMenu; 40 import javax.swing.JFrame; 41 import javax.swing.JMenuBar; 42 import javax.swing.JPanel; 43 import javax.swing.SwingConstants; 44 45 public class LabelDotTest 46 { 47 private final static String longText = "show a very long text to have it " + 48 "automatically shortened"; 49 private final static String shortText = "show short text"; 50 51 private static JFrame frame; 52 private static JLabel label; 53 private static JMenu menu; 54 private static volatile boolean isException = false; 55 56 private static void createUI() { 57 System.out.println("BEFORE CREATION"); 58 frame = new JFrame(); 59 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 60 frame.setSize(new Dimension(50, 150)); 61 frame.setLocationRelativeTo(null); 62 63 frame.setLayout(new GridBagLayout()); 64 GridBagConstraints c = new GridBagConstraints(); 65 c.fill = GridBagConstraints.BOTH; 66 c.weightx = 1.0; 67 c.weighty = 0.0; 68 c.gridwidth = GridBagConstraints.REMAINDER; 69 70 JMenuBar menuBar = new JMenuBar(); 71 menu = new JMenu("Menu"); 72 menuBar.add(menu); 73 frame.add(menuBar, c); 74 75 frame.add(new JLabel("Title", SwingConstants.CENTER), c); 76 77 c.weighty = 1.0; 78 frame.add(new JPanel(new GridBagLayout()), c); 79 c.weighty = 0.0; 80 81 label = new JLabel(shortText); 82 frame.add(label, c); 83 84 frame.setVisible(true); 85 } 86 87 private static void runTest(int iterations) throws Exception{ 88 Robot robot = new Robot(); 89 90 IntStream.range(0, iterations).forEach((i) -> { 91 SwingUtilities.invokeLater(() -> { 92 if (label.getText().equals(shortText)) { 93 label.setText(longText); 94 } else { 95 label.setText(shortText); 96 } 97 /* For a top level menu item, minimum size and the 98 preferred size should be the same, and should not be 99 equal to 1. Save the exception state and throw later 100 once the iterations are completed. 101 */ 102 isException = (menu.getMinimumSize().height == 1 && 103 !menu.getMinimumSize().equals(menu.getPreferredSize())) || 104 isException; 105 }); 106 robot.waitForIdle(); 107 }); 108 } 109 110 public static void main(String[] args) throws Exception { 111 try { 112 SwingUtilities.invokeAndWait(() -> createUI()); 113 runTest(50); 114 } finally { 115 SwingUtilities.invokeAndWait(() -> frame.dispose()); 116 if (isException) 117 throw new RuntimeException("Size of Menu bar is not correct."); 118 } 119 } 120 }