// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@

package example;

import java.awt.*;
import java.lang.reflect.Field;
import java.security.AccessController;
import java.security.PrivilegedAction;
import javax.swing.*;

public final class MainPanel extends JPanel {
  private MainPanel() {
    super();
    JLabel label = makeLabel("FORCE_HEAVYWEIGHT_POPUP", Color.PINK);

    ToolTipManager.sharedInstance().setLightWeightPopupEnabled(false);
    AccessController.doPrivileged(new PrivilegedAction<Void>() {
      @Override public Void run() {
        try {
          Field field;
          if (System.getProperty("java.version").startsWith("1.6.0")) {
            // https://community.oracle.com/thread/1357949 ComboBox scroll and selected/highlight on glasspane
            // Class<?> clazz = Class.forName("javax.swing.PopupFactory"); // errorprone: LiteralClassName
            // field = clazz.getDeclaredField("forceHeavyWeightPopupKey");
            field = PopupFactory.class.getDeclaredField("forceHeavyWeightPopupKey");
          } else { // JDK 1.7.0, 1.8.0
            Class<?> clazz = Class.forName("javax.swing.ClientPropertyKey");
            field = clazz.getDeclaredField("PopupFactory_FORCE_HEAVYWEIGHT_POPUP");
          }
          field.setAccessible(true);
          label.putClientProperty(field.get(null), Boolean.TRUE);
        } catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException ex) {
          throw new UnsupportedOperationException(ex);
        }
        return null;
      }
    });

    JComponent glass = new JPanel(new BorderLayout()) {
      private final Color backgroundColor = new Color(0x64_64_64_C8, true);
      @Override protected void paintComponent(Graphics g) {
        g.setColor(backgroundColor);
        g.fillRect(0, 0, getWidth(), getHeight());
        super.paintComponent(g);
      }
    };
    glass.setOpaque(false);
    glass.add(makeLabel("Default: ToolTipText", Color.ORANGE), BorderLayout.WEST);
    glass.add(label, BorderLayout.EAST);
    glass.add(Box.createVerticalStrut(60), BorderLayout.SOUTH);
    EventQueue.invokeLater(() -> {
      getRootPane().setGlassPane(glass);
      getRootPane().getGlassPane().setVisible(true);
    });
    setPreferredSize(new Dimension(320, 240));
  }

  private static JLabel makeLabel(String title, Color color) {
    JLabel label = new JLabel(title);
    label.setOpaque(true);
    label.setBackground(color);
    label.setToolTipText("1234567890");
    return label;
  }

  public static void main(String[] args) {
    EventQueue.invokeLater(MainPanel::createAndShowGui);
  }

  private static void createAndShowGui() {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
      ex.printStackTrace();
      Toolkit.getDefaultToolkit().beep();
    }
    JFrame frame = new JFrame("@title@");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.getContentPane().add(new MainPanel());
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}

@@ -504,20 +504,17 @@
 vileged(
-new 
+(
 Privileg
@@ -531,46 +531,15 @@
 oid%3E
-(
 ) 
-%7B%0A      @Override public Void run()
+() -%3E
  %7B%0A 
@@ -543,18 +543,16 @@
 %7B%0A      
- 
- 
 try %7B%0A  
@@ -557,17 +557,18 @@
         
- 
+//
  Field f
@@ -581,17 +581,18 @@
         
- 
+//
  if (Sys
@@ -650,25 +650,26 @@
 ) %7B%0A        
- 
+//
    // https:
@@ -762,25 +762,26 @@
 ane%0A        
- 
+//
    // Class%3C
@@ -841,14 +841,14 @@
  // 
-e
+E
 rror
-p
+P
 rone
@@ -870,25 +870,26 @@
 ame%0A        
- 
+//
    // field 
@@ -942,33 +942,34 @@
 pKey%22);%0A        
- 
+//
    field = Popup
@@ -1036,17 +1036,18 @@
         
- 
+//
  %7D else 
@@ -1051,28 +1051,8 @@
 se %7B
- // JDK 1.7.0, 1.8.0
 %0A   
@@ -1056,17 +1056,18 @@
         
- 
+//
    Class
@@ -1130,25 +1130,26 @@
 %22);%0A        
- 
+//
    field = c
@@ -1215,30 +1215,188 @@
 %22);%0A        
-  %7D%0A  
+// %7D%0A        Class%3C?%3E clazz = Class.forName(%22javax.swing.ClientPropertyKey%22);%0A        Field field = clazz.getDeclaredField(%22PopupFactory_FORCE_HEAVYWEIGHT_POPUP%22);%0A
         fiel
@@ -1426,18 +1426,16 @@
         
- 
- 
 label.pu
@@ -1482,18 +1482,16 @@
 .TRUE);%0A
-  
       %7D 
@@ -1570,26 +1570,24 @@
 ption ex) %7B%0A
-  
         thro
@@ -1633,22 +1633,18 @@
 ;%0A      
-  %7D%0A  
+%7D%0A
       re
@@ -1654,24 +1654,16 @@
 n null;%0A
-      %7D%0A
     %7D);%0A

anonymous new PrivilegedAction<Void>() can be replaced with lambda