Path: blob/master/test/jdk/javax/swing/JLabel/7004134/bug7004134.java
41153 views
/*1* Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @key headful26* @bug 700413427* @summary JLabel containing a ToolTipText does no longer show ToolTip after browser refresh28* @author Pavel Porvatov29* @modules java.desktop/sun.awt30* @modules java.desktop/javax.swing:open31*/3233import sun.awt.SunToolkit;3435import javax.swing.*;36import java.awt.*;37import java.awt.event.MouseEvent;38import java.lang.reflect.Field;3940public class bug7004134 {41private static volatile JFrame frame;4243private static volatile JLabel label;4445private static volatile int toolTipWidth;4647public static void main(String[] args) throws Exception {48SwingUtilities.invokeAndWait(new Runnable() {49public void run() {50label = new JLabel("A JLabel used as object for an HTML-formatted tooltip");51label.setToolTipText("<html><body bgcolor=FFFFE1>An HTML-formatted ToolTip</body></html>");5253frame = new JFrame();5455frame.add(label);56frame.pack();57frame.setVisible(true);58}59});6061((SunToolkit) SunToolkit.getDefaultToolkit()).realSync();6263SwingUtilities.invokeAndWait(new Runnable() {64public void run() {65ToolTipManager toolTipManager = ToolTipManager.sharedInstance();6667toolTipManager.setInitialDelay(0);68toolTipManager.mouseMoved(new MouseEvent(label, 0, 0, 0, 0, 0, 0, false));69}70});7172Thread.sleep(500);7374SwingUtilities.invokeAndWait(new Runnable() {75public void run() {76toolTipWidth = getTipWindow().getWidth();7778frame.dispose();79}80});8182Thread thread = new Thread(new ThreadGroup("Some ThreadGroup"), new Runnable() {83public void run() {84SunToolkit.createNewAppContext();8586try {87SwingUtilities.invokeAndWait(new Runnable() {88public void run() {89frame = new JFrame();9091frame.add(label);92frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);93frame.pack();94frame.setVisible(true);95}96});9798((SunToolkit) SunToolkit.getDefaultToolkit()).realSync();99100SwingUtilities.invokeAndWait(new Runnable() {101public void run() {102ToolTipManager toolTipManager = ToolTipManager.sharedInstance();103104toolTipManager.setInitialDelay(0);105toolTipManager.mouseMoved(new MouseEvent(label, 0, 0, 0, 0, 0, 0, false));106}107});108109Thread.sleep(500);110111SwingUtilities.invokeAndWait(new Runnable() {112public void run() {113int newToolTipWidth = getTipWindow().getWidth();114115frame.dispose();116117if (toolTipWidth != newToolTipWidth) {118throw new RuntimeException("Tooltip width is different. Initial: " + toolTipWidth +119", new: " + newToolTipWidth);120}121}122});123} catch (Exception e) {124throw new RuntimeException(e);125}126127}128});129130thread.start();131thread.join();132}133134private static Component getTipWindow() {135try {136Field tipWindowField = ToolTipManager.class.getDeclaredField("tipWindow");137138tipWindowField.setAccessible(true);139140Popup value = (Popup) tipWindowField.get(ToolTipManager.sharedInstance());141142Field componentField = Popup.class.getDeclaredField("component");143144componentField.setAccessible(true);145146return (Component) componentField.get(value);147} catch (Exception e) {148throw new RuntimeException("getToolTipComponent failed", e);149}150}151}152153154