Swing/ColorTab のバックアップ差分(No.23)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/ColorTab へ行く。
- 1 (2004-08-03 (火) 01:54:42)
- 2 (2004-09-06 (月) 09:54:10)
- 3 (2004-10-08 (金) 06:18:16)
- 4 (2004-10-14 (木) 03:57:00)
- 5 (2004-11-04 (木) 10:03:23)
- 6 (2004-12-21 (火) 10:38:09)
- 7 (2005-04-28 (木) 04:32:51)
- 8 (2005-11-11 (金) 19:47:27)
- 9 (2006-02-27 (月) 15:34:48)
- 10 (2006-04-12 (水) 19:38:24)
- 11 (2006-04-16 (日) 01:19:09)
- 12 (2006-11-10 (金) 03:39:31)
- 13 (2007-04-05 (木) 19:45:04)
- 14 (2007-05-25 (金) 14:38:13)
- 15 (2007-07-26 (木) 14:32:10)
- 16 (2008-01-16 (水) 19:08:51)
- 17 (2009-01-08 (木) 17:05:32)
- 18 (2012-01-05 (木) 21:31:08)
- 19 (2012-06-28 (木) 02:07:32)
- 20 (2013-04-04 (木) 02:27:39)
- 21 (2014-11-01 (土) 00:46:09)
- 22 (2014-12-24 (水) 14:45:44)
- 23 (2015-10-14 (水) 15:36:39)
- 24 (2017-04-13 (木) 12:08:33)
- 25 (2017-11-02 (木) 15:34:40)
- 26 (2018-03-20 (火) 13:45:07)
- 27 (2018-10-10 (水) 22:46:31)
- 28 (2019-05-22 (水) 19:34:28)
- 29 (2020-10-04 (日) 02:40:54)
- 30 (2022-07-07 (木) 14:20:26)
- 31 (2022-08-20 (土) 22:15:25)
- 追加された行はこの色です。
- 削除された行はこの色です。
--- title: JTabbedPaneの選択文字色を変更 tags: [JTabbedPane, ChangeListener] author: aterai pubdate: 2004-02-09 description: JTabbedPaneで、選択されたタブの文字色を変更します。 --- * 概要 [#q40bd72b] `JTabbedPane`で、選択されたタブの文字色を変更します。 #download(https://lh5.googleusercontent.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(); 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")) { 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; } else if (i == sindex) { Color sc = (sindex % 2 == 0) ? Color.RED : Color.BLUE; jtab.setForegroundAt(i, sc); }else{ } else { jtab.setForegroundAt(i, Color.BLACK); } } } }); }} * 解説 [#a0f82c36] `JTabbedPane`に`ChangeListener`を追加し、条件によってタブの文字色を変更しています。 背景色も変更したかったのですが、`Look and Feel`などによってはうまくいかないようです。`Windows XP`でタブの背景色を変更したい場合は、以下のように`System.setProperty("swing.noxp", "true")`とする必要があります。 #code{{ public static void createAndShowGUI() { System.setProperty("swing.noxp", "true"); try{ try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); }catch (Exception e) { } catch (Exception e) { throw new InternalError(e.toString()); } JFrame frame = new JFrame("@title@"); //...... }} ---- 文字色も`Synth(Nimbus)LookAndFeel`などでは、`JTabbedPane#setForegroundAt(...)`で変更することができないようです。 - [http://bugs.java.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 #comment