TITLE:JToolTipをGlassPane上のコンポーネントで表示する
#navi(../)
#tags()
RIGHT:Posted by &author(aterai); at 2009-05-11
*JToolTipをGlassPane上のコンポーネントで表示する [#d46a12cd]
JToolTipをGlassPane上のコンポーネントに追加した場合でも、手前に表示されるように設定します。主に[http://forums.sun.com/thread.jspa?threadID=5315492 Swing - ComboBox scroll and selected/highlight on glasspane]を参考にしています。

//-&jnlp;
-&jar;
-&zip;

//#screenshot
#ref(http://lh6.ggpht.com/_9Z4BYR88imo/TQTNMeZI4ZI/AAAAAAAAAaY/8XHy9j6jQw0/s800/ForceHeavyWeightPopupKey.png)

**サンプルコード [#f0477f43]
#code(link){{
//Swing - ComboBox scroll and selected/highlight on glasspane
//http://forums.sun.com/thread.jspa?threadID=5315492
try {
  Class clazz = Class.forName("javax.swing.PopupFactory");
  Field field = clazz.getDeclaredField("forceHeavyWeightPopupKey");
  field.setAccessible(true);
  label2.putClientProperty(field.get(null), Boolean.TRUE);
} catch (Exception ex) {
  ex.printStackTrace();
}
}}

**解説 [#rec54369]
上記のサンプルでは、ボタンをクリックすると、二つのラベルをもつGlassPaneが表示されます。

-111...(左)
--GlassPaneの下にJTooltipが表示される
--親フレームの外にJTooltipがはみ出す場合は、正常に表示される
--%%ToolTipManager.sharedInstance().setLightWeightPopupEnabled(false); では効果なし?%%
-222...(右)
--正常に表示されるように、常に、JTooltipを重量コンポーネントとして表示している
--PopupFactoryクラスの forceHeavyWeightPopupKey をリフレクションで取得して、JComponent#putClientPropertyメソッドで設定
--[http://forums.sun.com/thread.jspa?threadID=5315492 Swing - ComboBox scroll and selected/highlight on glasspane]のGlassPaneでJComboBoxのポップアップを正常に表示する方法を引用
--ToolTipManager.sharedInstance().setLightWeightPopupEnabled(false); としておかないと前面に表示されない環境がある?

----
JDK 1.7.0 の場合は、javax.swing.PopupFactory.forceHeavyWeightPopupKey が無くなってしまったので、以下のように
javax.swing.ClientPropertyKey.PopupFactory_FORCE_HEAVYWEIGHT_POPUP を使用します。
#code{{
Class clazz = Class.forName("javax.swing.ClientPropertyKey");
Field field = clazz.getDeclaredField("PopupFactory_FORCE_HEAVYWEIGHT_POPUP");
field.setAccessible(true);
combo.putClientProperty(field.get(null), Boolean.TRUE);
}}

**参考リンク [#webb6ab8]
-[http://forums.sun.com/thread.jspa?threadID=5315492 Swing - ComboBox scroll and selected/highlight on glasspane]
-[http://www.atmarkit.co.jp/bbs/phpBB/viewtopic.php?mode=viewtopic&topic=42615&forum=12 JComboBox の GlassPane 上でのレンダリング]
-[http://forums.sun.com/thread.jspa?threadID=5245072 Swing - Why glass pane requires setLightWeightPopupEnabled(false)?]
-[http://terai.xrea.jp/Swing/ModalInternalFrame.html JInternalFrameをModalにする]

**コメント [#n67c57a8]
#comment