• 追加された行はこの色です。
  • 削除された行はこの色です。
---
category: swing
folder: WindowOpacity
title: JFrameを半透明化
tags: [JFrame, Translucent, Transparent, JRootPane, ContentPane, TexturePaint]
author: aterai
pubdate: 2010-06-14T14:13:21+09:00
description: JFrameを半透明にします。
description: JFrameのタイトルや子コンポーネントを除く背景が半透明になるよう設定します。
image: https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTWw2d9LNI/AAAAAAAAApw/NXG2EcaSv_s/s800/WindowOpacity.png
hreflang:
    href: https://java-swing-tips.blogspot.com/2010/06/translucent-jframe.html
    lang: en
---
* 概要 [#ea286453]
`JFrame`を半透明にします。
* 概要 [#summary]
`JFrame`タイトルや子コンポーネントを除く背景が半透明になるよう設定します。

#download(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTWw2d9LNI/AAAAAAAAApw/NXG2EcaSv_s/s800/WindowOpacity.png)

* サンプルコード [#l5dd57cc]
* サンプルコード [#sourcecode]
#code(link){{
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame();
//com.sun.awt.AWTUtilities.setWindowOpacity(frame, .5f);
com.sun.awt.AWTUtilities.setWindowOpaque(frame, false);
// com.sun.awt.AWTUtilities.setWindowOpacity(frame, .5f);
// com.sun.awt.AWTUtilities.setWindowOpaque(frame, false); // Java 6
frame.setBackground(new Color(0x0, true));
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

JPanel p = new JPanel();
p.add(new JButton("JButton"));
p.setBackground(new Color(.5f, .8f, .5f, .5f));

frame.getContentPane().add(p);
frame.setSize(320, 240);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}}

* 解説 [#v5c228ee]
`com.sun.awt.AWTUtilities.setWindowOpacity(frame, .5f);`を使って半透明化すると、フレームのタイトルバーや、子コンポーネントまで半透明化されるので、代わりに上記のサンプルでは以下のようにして半透明化を行っています。
* 解説 [#explanation]
`com.sun.awt.AWTUtilities.setWindowOpacity(frame, .5f);`を使って`Window`を半透明化すると`JFrame`などのタイトルバーや子コンポーネントまで半透明化されるので、代わりに上記のサンプルでは以下のようにして半透明化を行っています。

- `JFrame.setDefaultLookAndFeelDecorated(true);`で、タイトルバーなどを`JRootPane`に描画
- `JFrame.setDefaultLookAndFeelDecorated(true);`でタイトルバーなどを`JRootPane`に描画
- `com.sun.awt.AWTUtilities.setWindowOpaque(frame, false);`で`JFrame`を完全に透明化
-- `JDK 1.7.0`の場合は、代わりに、`frame.setBackground(new Color(0,0,0,0));`
- `ContentPane`に`setBackground(new Color(.5f,.8f,.5f,.5f));`で半透明の背景色を設定したパネルを追加
-- `JDK 1.7.0`の場合は、代わりに`frame.setBackground(new Color(0x0, true));`
- `ContentPane`に`setBackground(new Color(.5f, .8f, .5f, .5f));`で半透明の背景色を設定したパネルを追加

* 参考リンク [#pc13cea7]
* 参考リンク [#reference]
- [http://www.viva-edo.com/komon/edokomon.html 江戸の文様(和風素材・デスクトップ壁紙)]
//- [http://java.sun.com/developer/technicalArticles/GUI/translucent_shaped_windows/ How to Create Translucent and Shaped Windows]
- [http://docs.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html How to Create Translucent and Shaped Windows (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)]
// - [http://java.sun.com/developer/technicalArticles/GUI/translucent_shaped_windows/ How to Create Translucent and Shaped Windows]
- [https://docs.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html How to Create Translucent and Shaped Windows (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)]
- [[Robotを使用してスクリーンショットを取得する>Swing/ScreenCapture]]

* コメント [#d57b3c09]
* コメント [#comment]
#comment
- そんな簡単にできるんですね!昔画面キャプチャーしたり色々苦労した結果断念しました; -- &user(riki); &new{2010-06-14 (月) 22:57:11};
-- `AWTUtilities.setWindowOpaque`などが使えるようになったのは、`6u10`からですが、上記のサンプルみたいなことができるようになったのは、`6u14`から(多分[http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6683775 Bug ID: 6683775 Painting artifacts is seen when panel is made setOpaque(false) for a translucent window])みたいですから、最近(ちょうど一年ぐらい)のようです。 -- &user(aterai); &new{2010-06-15 (火) 13:26:11};
-- `AWTUtilities.setWindowOpaque`などが使えるようになったのは、`6u10`からですが、上記のサンプルみたいなことができるようになったのは、`6u14`から(多分[https://bugs.openjdk.org/browse/JDK-6683775 Bug ID: 6683775 Painting artifacts is seen when panel is made setOpaque(false) for a translucent window])みたいですから、最近(ちょうど一年ぐらい)のようです。 -- &user(aterai); &new{2010-06-15 (火) 13:26:11};
- 「`Windows 7` + `JDK 1.7.0`」で、このサンプルにある`JComboBox`のドロップダウンリストが正常に描画されない? 「`Windows XP` + `JDK 1.7.0`」や、「`Windows 7` + `JDK 1.6.0_27`」は問題なし。 -- &user(aterai); &new{2011-10-18 (火) 19:01:43};
- `JDK 1.7.0`では、背景を切り替えたときに前の背景の残像が残る場合がある? -- &user(aterai); &new{2011-10-18 (火) 19:06:07};
-- こちらは`((JFrame)w).getContentPane().repaint();`でうまく%%いくが理由が不明…%% ようなので修正。 -- &user(aterai); &new{2011-10-18 (火) 19:19:00};
-- こちらは`((JFrame) w).getContentPane().repaint();`でうまく%%いくが理由が不明…%% ようなので修正。 -- &user(aterai); &new{2011-10-18 (火) 19:19:00};
- `JDK 1.7.0_04`で?、透明にした`JFrame`に`JComboBox`を追加すると、ドロップダウンリストがおかしい? -- &user(aterai); &new{2012-05-15 (火) 12:49:23};
-- 以下のような`PopupMenuListener`を追加すれば回避できるが…。 -- &user(aterai); &new{2012-05-15 (火) 14:16:17};
#code{{
combo.addPopupMenuListener(new PopupMenuListener() {
  @Override public void popupMenuWillBecomeVisible(final PopupMenuEvent e) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        JComboBox c = (JComboBox) e.getSource();
        Object o = c.getAccessibleContext().getAccessibleChild(0);
        if (o instanceof JComponent) { //BasicComboPopup
          ((JComponent) o).repaint();
        }
  @Override public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
    EventQueue.invokeLater(() -> {
      JComboBox<?> c = (JComboBox<?>) e.getSource();
      Object o = c.getAccessibleContext().getAccessibleChild(0);
      if (o instanceof JComponent) { // BasicComboPopup
        ((JComponent) o).repaint();
      }
    });
  }
  @Override public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {}
  @Override public void popupMenuCanceled(PopupMenuEvent e) {}

  @Override public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
    /* not needed */
  }

  @Override public void popupMenuCanceled(PopupMenuEvent e) {
    /* not needed */
  }
});
}}

-- `7u6`では修正されている。`JDK 1.7.0_06`の`Bug Fixes`に載っている [http://bugs.java.com/bugdatabase/view_bug.do?bug_id=7156657 Bug ID: 7156657 Version 7 doesn't support translucent popup menus against a translucent window] が関係している気がするけど、`Release Fixed`に、`7u6`が無い? -- &user(aterai); &new{2012-09-19 (水) 18:18:48};
--- バックポートされた[http://bugs.java.com/bugdatabase/view_bug.do?bug_id=2224554 Bug ID: JDK-2224554 Version 7 doesn't support translucent popup menus against a translucent window]に、`7u6`があった。 -- &user(aterai); &new{2013-10-29 (火) 19:26:30};
-- `7u6`では修正されている。`JDK 1.7.0_06`の`Bug Fixes`に載っている [https://bugs.openjdk.org/browse/JDK-7156657 Bug ID: 7156657 Version 7 doesn't support translucent popup menus against a translucent window] が関係している気がするけど、`Release Fixed`に、`7u6`が無い? -- &user(aterai); &new{2012-09-19 (水) 18:18:48};
--- バックポートされた[https://bugs.openjdk.org/browse/JDK-2224554 Bug ID: JDK-2224554 Version 7 doesn't support translucent popup menus against a translucent window]に、`7u6`がある。 -- &user(aterai); &new{2013-10-29 (火) 19:26:30};

#comment