Question

in my program I override the functions getForeGroundAt and getBackgroundAt for the tabbed pane as such:

jtp = new JTabbedPane(SwingConstants.LEFT) {  
    public Color getForegroundAt(int index) {               
        if (getSelectedIndex() == index) {
            System.out.println("ueue1");
            return new Color(69, 69, 69); 
        }
        return new Color(188, 188, 188);  
    } 
    public Color getBackgroundAt(int index) {
        if (getSelectedIndex() == index) {
            System.out.println("ueue2");
            return new Color(247, 248, 243);
        }
        return new Color(255, 255, 255);  
    } 
};

The problem I am facing is that the print statement in the getBackgrountAt method is never being called even when I select different tabs while the one in the setForegroundAt is acting normally. The result is that I am not able to set the background colour of the selected tab but I can set the foreground colour. How do I solve this problem? Thanks.

Was it helpful?

Solution

This is a LAF feature. The BasicTabbedPaneUI has code like:

Color selectedColor = UIManager.getColor("TabbedPane.selected");
...
...
g.setColor(!isSelected || selectedColor == null ? 
    tabPane.getBackgroundAt(tabIndex) : selectedColor);

So the property in the UIManager has priority. You can try to update the UIManager with the Color that you want:

UIManager.put("TabbedPane.selected", ...);

See UIManager Defaults for more information and a list of the UIManager properties. The foreground doesn't have this problem because there is no default property for the selected foreground.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top