JToolTipをGlassPane上のコンポーネントで表示する
Total: 7965, Today: 1, Yesterday: 3
Posted by aterai at
Last-modified:
Summary
JToolTipをGlassPane上のコンポーネントに追加した場合でも、手前に表示されるように設定します。主にSwing - ComboBox scroll and selected/highlight on glasspaneを参考にしています。
Screenshot

Advertisement
Source Code Examples
// Swing - ComboBox scroll and selected/highlight on glasspane
// https://community.oracle.com/thread/1357949
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();
}
View in GitHub: Java, KotlinDescription
上記のサンプルでは、ボタンをクリックすると、二つのラベルをもつGlassPaneが表示されます。
111...(左)GlassPaneの下にJToolTipが表示される- 親フレームの外に
JToolTipがはみ出す場合は、正常に表示される ToolTipManager.sharedInstance().setLightWeightPopupEnabled(false);では効果なし?
222...(右)- 正常に表示されるように常に
JToolTipを重量コンポーネントとして表示している PopupFactoryクラスのforceHeavyWeightPopupKeyをリフレクションで取得してJComponent#putClientPropertyメソッドで設定- 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を使用する必要がある
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);
Java 9以降では以下のようなコードでPopupを常にHeavyWeightで開くことが可能になった
PopupFactory.setSharedInstance(new PopupFactory() {
@Override public Popup getPopup(Component owner, Component contents, int x, int y) throws IllegalArgumentException {
// @param isHeavyWeightPopup true if Popup should be heavy weight,
// protected Popup getPopup(..., boolean isHeavyWeightPopup) ...
return super.getPopup(owner, contents, x, y, true);
}
});
Reference
- Swing - ComboBox scroll and selected/highlight on glasspane
- JComboBox の GlassPane 上でのレンダリング
- Swing - Why glass pane requires setLightWeightPopupEnabled(false)?
- JInternalFrameをModalにする
- Mixing Heavyweight and Lightweight Components
- JPopupMenuを常に重量ポップアップで開く