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