• 追加された行はこの色です。
  • 削除された行はこの色です。
#navi(../)
*JTabbedPaneの選択文字色を変更 [#q40bd72b]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2004-02-09~
更新日:&lastmod;
---
category: swing
folder: ColorTab
title: JTabbedPaneの選択文字色を変更
tags: [JTabbedPane, ChangeListener]
author: aterai
pubdate: 2004-02-09
description: JTabbedPaneで、選択されたタブの文字色を変更します。
image: https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTJo7nZnbI/AAAAAAAAAUs/6SU2JG2B0t0/s800/ColorTab.png
---
* 概要 [#summary]
`JTabbedPane`で、選択されたタブの文字色を変更します。

#contents
#download(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTJo7nZnbI/AAAAAAAAAUs/6SU2JG2B0t0/s800/ColorTab.png)

**概要 [#qaaf3ed4]
JTabbedPaneで、選択されたタブの文字色を変更します。
* サンプルコード [#sourcecode]
#code(link){{
tabbedPane.addChangeListener(e -> {
  JTabbedPane tabs = (JTabbedPane) e.getSource();
  int sindex = tabs.getSelectedIndex();
  String str = tabs.getTitleAt(sindex);
  for (int i = 0; i < tabs.getTabCount(); i++) {
    if (i == sindex && tabs.getTitleAt(sindex).endsWith("1")) {
      tabs.setForegroundAt(i, Color.GREEN);
    } else if (i == sindex) {
      Color sc = (sindex % 2 == 0) ? Color.RED : Color.BLUE;
      tabs.setForegroundAt(i, sc);
    } else {
      tabs.setForegroundAt(i, Color.BLACK);
    }
  }
});
}}

#screenshot
* 解説 [#explanation]
`JTabbedPane`に`ChangeListener`を追加し、条件によってタブの文字色を変更しています。

**サンプルコード [#g98608e3]
 tab.addChangeListener(new ChangeListener() {
   public void stateChanged(ChangeEvent e) {
     JTabbedPane jtab = (JTabbedPane)e.getSource();
     jtab.setVisible(false);
     int sindex = jtab.getSelectedIndex();
     String str = jtab.getTitleAt(sindex);
     for(int i=0;i<jtab.getTabCount();i++) {
       if(i==sindex && "タイトル1".equals(str)) {
         //jtab.setBackgroundAt(i, Color.green);
         jtab.setForegroundAt(i, Color.green);
       }else if(i==sindex) {
         Color sc = (sindex%2==0)?Color.red:Color.blue;
         //jtab.setBackgroundAt(i, sc);
         jtab.setForegroundAt(i, sc);
       }else{
         //jtab.setBackgroundAt(i, null);
         jtab.setForegroundAt(i, Color.black);
       }
     }
     jtab.setVisible(true);
   }
 });
- タブ文字色: `JTabbedPane#setForegroundAt(Color)`
-- `Look and Feel`に依存する(`JDK 1.7.0`からドキュメントに追記された)
-- `Synth(Nimbus)LookAndFeel`などでは`JTabbedPane#setForegroundAt(Color)`メソッドは無効
-- [https://bugs.openjdk.org/browse/JDK-6939001 Bug ID: 6939001 Nimbus: JTabbedPane setBackgroundAt and setForegroundAt have no effect]
- タブ背景色: `JTabbedPane#setBackgroundAt(Color)`
-- `Look and Feel`に依存する(`JDK 1.7.0`からドキュメントに追記された)
-- `Windows XP`でタブの背景色を変更したい場合は、以下のように`System.setProperty("swing.noxp", "true")`と設定する必要がある

-&jnlp;
-&jar;
-&zip;
#code{{
public static void createAndShowGUI() {
  System.setProperty("swing.noxp", "true");
  try {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  } catch (Exception e) {
    throw new InternalError(e.toString());
  }
  JFrame frame = new JFrame("@title@");
// ...
}}

**解説 [#a0f82c36]
新たにChangeListenerを追加し、条件によってタブの文字色を変更しています。
* 参考リンク [#reference]
- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/JTabbedPane.html#setForegroundAt-int-java.awt.Color- JTabbedPane#setForegroundAt(Color) (Java Platform SE 8)]
- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/JTabbedPane.html#setBackgroundAt-int-java.awt.Color- JTabbedPane#setBackgroundAt(Color) (Java Platform SE 8)]
- [[JTabbedPaneのタブ文字列をハイライト>Swing/TabTitleHighlight]]

背景色も変更したかったのですが、Look & Feelなどによってはうまくいかないようです。WIndows XPでタブの背景色を変更したい場合は、以下のようにSystem.setProperty("swing.noxp", "true")とする必要があります。
 public static void createAndShowGUI() {
   System.setProperty("swing.noxp", "true");
   try{
     UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
   }catch (Exception e) {
     throw new InternalError(e.toString());
   }
   final JFrame frame = new JFrame("@title@");
   (以下省略)

**参考リンク [#l67a03ff]
-[[TabColorExample>http://www.physci.org/codes/display.jsp?fl=%2Fcodes%2Ftame%2Ftame%2Fexamples%2FTabColorExample.java]]

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