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.util.stream.IntStream;
  34 
  35 import javax.swing.SwingUtilities;
  36 import javax.swing.JLabel;
  37 import javax.swing.JMenu;
  38 import javax.swing.JFrame;
  39 import javax.swing.JMenuBar;
  40 import javax.swing.JPanel;
  41 import javax.swing.SwingConstants;
  42 
  43 public class LabelDotTest
  44 {
  45     private final static String longText = "show a very long text to have it " +
  46             "automatically shortened";
  47     private final static String shortText = "show short text";
  48 
  49     public LabelDotTest(int seconds) {
  50         System.out.println("BEFORE CREATION");
  51         JFrame frame = new JFrame();
  52         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  53         frame.setSize(new Dimension(50, 150));
  54         frame.setLocationRelativeTo(null);
  55 
  56         frame.setLayout(new GridBagLayout());
  57         GridBagConstraints c = new GridBagConstraints();
  58         c.fill = GridBagConstraints.BOTH;
  59         c.weightx = 1.0;
  60         c.weighty = 0.0;
  61         c.gridwidth = GridBagConstraints.REMAINDER;
  62 
  63         JMenuBar menuBar = new JMenuBar();
  64         JMenu menu = new JMenu("Menu");
  65         menuBar.add(menu);
  66         frame.add(menuBar, c);
  67 
  68         frame.add(new JLabel("Title", SwingConstants.CENTER), c);
  69 
  70         c.weighty = 1.0;
  71         frame.add(new JPanel(new GridBagLayout()), c);
  72         c.weighty = 0.0;
  73 
  74         JLabel label = new JLabel(shortText);
  75         frame.add(label, c);
  76 
  77         frame.setVisible(true);
  78 
  79         IntStream.range(0, seconds).forEach((i) -> {
  80             SwingUtilities.invokeLater(() -> {
  81                 if ( label.getText().equals(shortText) ) {
  82                     label.setText(longText);
  83                 } else {
  84                     label.setText(shortText);
  85                 }
  86             /*
  87                 For a top level menu item, minimum size and the preferred
  88                 size should be the same, and should not be equal to 1
  89              */
  90                 if (menu.getMinimumSize().height == 1 &&
  91                         !menu.getMinimumSize().equals(menu.getPreferredSize()))
  92                     throw new RuntimeException("Size of Menu bar is not correct.");
  93             });
  94 
  95             try {
  96                 Thread.sleep(1000);
  97             }
  98             catch( InterruptedException e ) { }
  99         });
 100 
 101         frame.dispose();
 102     }
 103 
 104     public static void main(String[] args) throws Exception {
 105         new LabelDotTest(10);
 106     }
 107 }