---
category: swing
folder: TranslucentTabbedPane
title: JTabbedPaneのタブなどを半透明にする
tags: [JTabbedPane, UIManager, LookAndFeel]
author: aterai
pubdate: 2016-03-14T00:04:15+09:00
description: JTabbedPaneのタブ、タブエリア、コンテンツエリアなどを半透明に設定します。
image: https://lh3.googleusercontent.com/-GLtzHl48JaY/VuWAq1hSxSI/AAAAAAAAOQk/HbrQluUnNH8_5fTM2gOIHhcoJMU21hmEgCCo/s800-Ic42/TranslucentTabbedPane.png
---
* Summary [#summary]
`JTabbedPane`のタブ、タブエリア、コンテンツエリアなどを半透明に設定します。

#download(https://lh3.googleusercontent.com/-GLtzHl48JaY/VuWAq1hSxSI/AAAAAAAAOQk/HbrQluUnNH8_5fTM2gOIHhcoJMU21hmEgCCo/s800-Ic42/TranslucentTabbedPane.png)

* Source Code Examples [#sourcecode]
#code(link){{
Color bgc = new Color(110, 110, 0, 100);
Color fgc = new Color(255, 255, 0, 100);
UIManager.put("TabbedPane.shadow", fgc);
UIManager.put("TabbedPane.darkShadow", fgc);
UIManager.put("TabbedPane.light", fgc);
UIManager.put("TabbedPane.highlight", fgc);
UIManager.put("TabbedPane.tabAreaBackground", fgc);
UIManager.put("TabbedPane.unselectedBackground", fgc);
UIManager.put("TabbedPane.background", bgc);
UIManager.put("TabbedPane.foreground", Color.WHITE);
UIManager.put("TabbedPane.focus", fgc);
UIManager.put("TabbedPane.contentAreaColor", fgc);
UIManager.put("TabbedPane.selected", fgc);
UIManager.put("TabbedPane.selectHighlight", fgc);
// UIManager.put("TabbedPane.borderHighlightColor", fgc);
// Maybe typo but defined in MetalTabbedPaneUI
UIManager.put("TabbedPane.borderHightlightColor", fgc);
}}

* Explanation [#explanation]
- `UIManager.put(...)`を使用して、`JTabbedPane`のタブやコンテンツ領域などに半透明の色を設定
- `Swing`のコンポーネントは背景色が半透明かどうかを判断して再描画しているわけではないため、例えばマウスカーソルの下の領域を再描画するイベントなどで背景色が重複上書きされて残像が残ったり、次第に色が濃くなる現象などが発生する
-- 半透明色で全体を再描画するように`JPanel#paintComponent(...)`をオーバーライドしたコンポーネントを挟んで回避する必要がある
-- [https://tips4java.wordpress.com/2009/05/31/backgrounds-with-transparency/ Backgrounds With Transparency « Java Tips Weblog]
-- [[JFrameの透明化と再描画>Swing/TranslucentFrameRepaint]]
- `UIManager.put(...)`で半透明になるかどうかは`LookAndFeel`に依存する
-- `NimbusLookAndFeel`ではタブなどが半透明にならない
-- `Classic`ではない`WindowsLookAndFeel`では半透明にならない
- `TabbedPane.borderHightlightColor`は`TabbedPane.borderHighlightColor`の書き間違いだと思うが、`MetalTabbedPaneUI`では`TabbedPane.borderHightlightColor`が定義されている

* Reference [#reference]
- [https://tips4java.wordpress.com/2009/05/31/backgrounds-with-transparency/ Backgrounds With Transparency « Java Tips Weblog]
- [[JTabbedPaneのタブエリア背景色などをテスト>Swing/TabAreaBackground]]

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