Here is my solution:
1. Customize a UI for the tabbed pane. Subclass javax.swing.plaf.basic.
BasicTabbedPaneUI and override its paintTabBackground method.
--------------------------------------------------------------------
import javax.swing.plaf.basic.BasicTabbedPaneUI;
import javax.swing.UIManager;
import java.awt.*;
public class MyTabbedPaneUI extends BasicTabbedPaneUI
{
// default color is decided by the Default UI.
Color selectedTabBgColor =
UIManager.getColor("TabbedPane.lightHighlight");
Insets contentBorderInsets = new Insets(1, 0, 0, 0);
public MyTabbedPaneUI(Color color){
selectedTabBgColor = color;
}
public void setSelectedTabBgColor(Color color){
selectedTabBgColor = color;
}
// Overriding method. defines the offset from the top of the
// tabbed pane to the component it contains.
protected Insets getContentBorderInsets(int tabPlacement) {
return contentBorderInsets;
}
// Overriding method. paints the background of the tabs.
protected void paintTabBackground(Graphics g, int tabPlacement,
int tabIndex,
int x, int y, int w, int h,
boolean isSelected ) {
g.setColor(tabPane.getBackgroundAt(tabIndex));
// these two lines are all that makes this method
// different from the paintTabBackground() in
// BasicTabbedPaneUI. we want to force the background
// color of the tab to be selectedTabBgColor, not
// the default one from the tabbed pane. (light gray)
if(isSelected)
g.setColor(selectedTabBgColor);
switch(tabPlacement) {
case LEFT:
g.fillRect(x+1, y+1, w-2, h-3);
break;
case RIGHT:
g.fillRect(x, y+1, w-2, h-3);
break;
case BOTTOM:
g.fillRect(x+1, y, w-3, h-1);
break;
case TOP:
default:
g.fillRect(x+1, y+1, w-3, h-1);
}
}
}
---------------------------------------------------------------------
2. Install the UI to your tabbedPane.
tabPane.setUI(new MyTabbedPaneUI(myInitialColor));
---------------------------------------------------------------------
3. You can change the color of the tab at run time by:
((MyTabbedPaneUI)tabPane.getUI()).setSelectedTabBgColor(newBackgroundColor);
---------------------------------------------------------------------
I may have copied this code from somewhere else. So I can not take
credit for it. Anyway, hope this helps.
Mingjian Song
> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On Behalf Of paul
> beckett (JIC)
> Sent: Thursday, September 05, 2002 12:39 PM
> To: '[EMAIL PROTECTED]'
> Subject: Altering the color of the selected tab (JTabbedPane)
> at runtime
> Importance: High
>
>
> How can I alter the color of the selected tab in a
> JTabbedPane at runtime.
> The following works if done before the creation of the
> JTabbedPane, but I need to be able to configure the colour
> scheme later.
>
> UIManager.put("TabbedPane.selected", Color.black);
>
> Cheers,
>
> Paul
> _______________________________________________
> Swing mailing list
> [EMAIL PROTECTED]
> http://eos.dk/mailman/listinfo/swing
>
_______________________________________________
Swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/swing