2011-10-26 8 views
0

BasicTabbedPaneUiを拡張して、私自身のtabPaneをデザインすることができます。私は、HTMLのテキストに1つの問題がある、タブが選択された後にテキストの色を設定することです。私は、次のようにのorignal方法とほぼ同じコードpaintTextメソッドをオーバーライド:私たちは、タブ用のHTMLテキストを持っている場合はBasicTabbedPaneUIペイントhtmlテキスト

@Override 
protected void paintText(Graphics g, int tabPlacement, Font font, FontMetrics metrics, int tabIndex, String title, Rectangle textRect, boolean isSelected) { 
    g.setFont(font); 

    View v = getTextViewForTab(tabIndex); 
    if (v != null) { 
     // html 
     Color fg = tabPane.getForegroundAt(tabIndex); 
     if (isSelected && (fg instanceof UIResource)) { 
      Color selectedFG = UIManager.getColor(
        "TabbedPane.selectedForeground"); 
      if (selectedFG != null) { 
       fg = selectedFG; 
      } 
     } 
     v.paint(g, textRect); 
    } else { 
     // plain text 
     int mnemIndex = tabPane.getDisplayedMnemonicIndexAt(tabIndex); 

     if (tabPane.isEnabled() && tabPane.isEnabledAt(tabIndex)) { 
      Color fg = tabPane.getForegroundAt(tabIndex); 
      if (isSelected && (fg instanceof UIResource)) { 
       Color selectedFG = UIManager.getColor(
         "TabbedPane.selectedForeground"); 
       if (selectedFG != null) { 
        fg = selectedFG; 
       } 
      } 
      g.setColor(fg); 
      SwingUtilities2.drawStringUnderlineCharAt(tabPane, g, 
        title, mnemIndex, 
        textRect.x, textRect.y + metrics.getAscent()); 

     } else { // tab disabled 
      g.setColor(tabPane.getBackgroundAt(tabIndex).brighter()); 
      SwingUtilities2.drawStringUnderlineCharAt(tabPane, g, 
        title, mnemIndex, 
        textRect.x, textRect.y + metrics.getAscent()); 
      g.setColor(tabPane.getBackgroundAt(tabIndex).darker()); 
      SwingUtilities2.drawStringUnderlineCharAt(tabPane, g, 
        title, mnemIndex, 
        textRect.x - 1, textRect.y + metrics.getAscent() - 1); 

     } 
    } 
} 

を、それvがnullではありません。私が塗りつぶしメソッドで使用されるグラフィックスオブジェクトに色を設定しても、テキストの色は変わりません。 タブのテキストを2行にしたいので、htmlを使用します。 色を変更していただきありがとうございます。

+0

HTML [color](http://www.w3.org/TR/REC-html32)属性を設定するとどうなりますか? – trashgod

+0

これはhtmlタグで直接設定すると機能しますが、タブを選択したときにどのように変更できますか? – xtrem06

答えて

1

完全なLook &フィールインプリメントの一部でない限り、カスタムを開発することには消極的です。

代わりに、TabComponentsDemoに表示され、How to Use Tabbed Panesで説明されているカスタムタブコンポーネントを考えます。これにより、ユーザが選択したLook & Feelとの互換性を犠牲にすることなく、コンポーネントの外観を完全に制御できます。

+0

+1のルックアンドフィールセージ – mKorbel

関連する問題