TITLE:JTabbedPaneの選択文字色を変更
#navi(../)
#tags()
RIGHT:Posted by &author(aterai); at 2004-02-09
*JTabbedPaneの選択文字色を変更 [#q40bd72b]
JTabbedPaneで、選択されたタブの文字色を変更します。

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

//#screenshot
#ref(http://lh5.ggpht.com/_9Z4BYR88imo/TQTJo7nZnbI/AAAAAAAAAUs/6SU2JG2B0t0/s800/ColorTab.png)

**サンプルコード [#g98608e3]
#code(link){{
tab.addChangeListener(new ChangeListener() {
  @Override public void stateChanged(ChangeEvent e) {
    JTabbedPane jtab = (JTabbedPane)e.getSource();
    int sindex = jtab.getSelectedIndex();
    String str = jtab.getTitleAt(sindex);
    for(int i=0;i<jtab.getTabCount();i++) {
      if(i==sindex && jtab.getTitleAt(sindex).endsWith("1")) {
        jtab.setForegroundAt(i, Color.GREEN);
      }else if(i==sindex) {
        Color sc = (sindex%2==0)?Color.RED:Color.BLUE;
        jtab.setForegroundAt(i, sc);
      }else{
        jtab.setForegroundAt(i, Color.BLACK);
      }
    }
  }
});
}}

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

背景色も変更したかったのですが、Look and Feelなどによってはうまくいかないようです。Windows XPでタブの背景色を変更したい場合は、以下のようにSystem.setProperty("swing.noxp", "true")とする必要があります。
#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@");
//......
}}

----
文字色もSynth(Nimbus) Look and Feel などでは、JTabbedPane#setForegroundAt(...)で変更することができないようです。

- [http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6939001 Bug ID: 6939001 Nimbus: JTabbedPane setBackgroundAt and setForegroundAt have no effect]

**参考リンク [#l67a03ff]
-[http://www.crionics.com/products/opensource/faq/swing_ex/JTabbedPaneExamples1.html TabColorExample]
-[[JTabbedPaneのタブ文字列をハイライト>Swing/TabTitleHighlight]]

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