Try this:
import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.basic.BasicTabbedPaneUI;
public class TabHighlight extends JPanel
{
private static final int MAX = 5;
private JTabbedPane pane = new JTabbedPane();
public TabHighlight()
{
for (int i = 0; i < MAX; i++)
{
Color color = Color.black; //Set default tab background to black
pane.add("Tab " + String.valueOf(i), new TabContent(pane, i, color));
pane.setBackgroundAt(i, color);
pane.setForegroundAt(i, Color.white);
}
this.add(pane);
}
private static class TabContent extends JPanel
{
private TabContent(JTabbedPane panel, int i, Color color)
{
//Active and inactive tab coloring must be done
//when rendering the CONTENT of the JTabbedPane object
//Override these default settings to allow for active and inactive
//tab background color changing
panel.setUI(new BasicTabbedPaneUI()
{
@Override
protected void installDefaults()
{
super.installDefaults();
highlight = Color.lightGray;
lightHighlight = Color.white;
shadow = Color.gray;
darkShadow = Color.darkGray;
//Active tab background is white
//so set the focus color to white
//to hide the active tab focus
//marker seeing that we want the
//active tab backgound to be different
focus = Color.black;
}
});
//Set the active tab background to white
UIManager.put("TabbedPane.selected", Color.gray);
UIManager.put("TabbedPane.unselectedTabBackground", Color.black);
//Remove borders
UIManager.put("TabbedPane.contentBorderInsets", new Insets(0, 0, 0, 0));
setOpaque(true);
setBackground(color);
setForeground(Color.white);
add(new JLabel("Tab content " + String.valueOf(i)));
}
@Override
public Dimension getPreferredSize()
{
return new Dimension(320, 240);
}
}
public void display()
{
JFrame f = new JFrame("TabHighlight");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
//Use this function here to change the look and feel to Java's default
//If using a mac with OS X Yosemite or another recent Mac OS X release
public static void initLookAndFeel()
{
try
{
UIManager.setLookAndFeel(
UIManager.getCrossPlatformLookAndFeelClassName()
);
}
catch(UnsupportedLookAndFeelException e)
{
}
catch(ClassNotFoundException e)
{
}
catch(InstantiationException e)
{
}
catch(IllegalAccessException e)
{
}
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
initLookAndFeel();
new TabHighlight().display();
}
});
}
}
Let's take take a look at this snippet of code that adds 5 tabs a program start up:
public TabHighlight()
{
for (int i = 0; i < MAX; i++)
{
Color color = Color.black; //Set default tab background to black
Color color2 = Color.white; //Set default tab foreground to white
pane.add("Tab " + String.valueOf(i), new TabContent(pane, i, color));
pane.setBackgroundAt(i, color);
pane.setForegroundAt(i, Color.white);
}
this.add(pane);
}
The first area that you are requesting help with your in program is in the "for" block. I will try to explain this in a way that you will understand.
The first two lines define the default background and foreground color for each inactive tab. You can change these to a color of your preference.
The third line adds a tab with the following attributes:
- A string defining the text that will be displayed on the tab
- A tab number, you don't have to include this if you don't want to
- Creates the second snippet of code which I will get to after this one
The fourth line applies the default background color
The final line applies the default foreground color. NOTE: these attributes cannot be changed with UIManager
. I have tried a few alternative approaches but was unsuccessful. Darn, I wish there was an easier way to achieve this.
Now let's take a closer look at this snippit of code that defines the appearance of active and inactive tabs:
private TabContent(JTabbedPane panel, int i, Color color)
{
//Active and inactive tab coloring must be done
//when rendering the CONTENT of the JTabbedPane object
//Override these default settings to allow for active and inactive
//tab background color changing
panel.setUI(new BasicTabbedPaneUI()
{
@Override
protected void installDefaults()
{
super.installDefaults();
highlight = Color.lightGray;
lightHighlight = Color.white;
shadow = Color.gray;
darkShadow = Color.darkGray;
//Active tab background is white
//so set the focus color to white
//to hide the active tab focus
//marker seeing that we want the
//active tab backgound to be different
focus = Color.black;
}
});
//Set the active tab background to white
UIManager.put("TabbedPane.selected", Color.gray);
//Set the inactive tab background to black
UIManager.put("TabbedPane.unselectedTabBackground", Color.black);
//Remove borders
UIManager.put("TabbedPane.contentBorderInsets"
, new Insets(0, 0, 0, 0));
setOpaque(true);
setBackground(color);
setForeground(Color.white);
add(new JLabel("Tab content " + String.valueOf(i)));
}
In this part of the code, do the following:
Override the UI by using this block of code below
panel.setUI(new BasicTabbedPaneUI()
{
//place the code from step 2 here
}
Enter these properties of that section:
highlight = Color.lightGray;
lightHighlight = Color.white;
shadow = Color.gray;
darkShadow = Color.darkGray;
focus = Color.black;
Just like in the first snippet, you change these attributes as well. Here is a little
tip: each tab has a little dotted box around the tab label, this marks the focus. If
you want to hide this marker, simply set its color to match that of the active tab,
which now has the focus.
Use the UIManager
to change the following attributes:
UIManager.put("TabbedPane.selected", Color.gray);
UIManager.put("TabbedPane.unselectedTabBackground", Color.black);
This will allow you change active and inactive tab backgrounds during program
runtime.
You were using the UIManager
which is the best way to achieve what you were trying
to do. Using this bit of code here allows you to make the changes that you looking
for, but you have do step 2 before any of these changes will be able to take
effect. UIManager.put("TabbedPane.selectedForeground", Color.xxx)
and
UIManager.put("TabbedPane.unselectedTabForeground", Color.xxx)
don't change the
foreground color, the foreground color stays the same.
UIManager.put("TabbedPane.selected", Color.xxx)
will change the background color
of the active tab and UIManager.put("TabbedPane.unselectedTabBackground")
will
change the background color of inactive tab.
Copy and paste these two snippets of code in to your file and change them as you need.
I would recommend that you copy the whole source code at the top and paste it into compiler and then run it first. This way you will be able see what this program does and when you go back to your original code that you are working with, you will know where to place these snippets of code.
Hope this helps.
If you are having trouble with this, post an SSCCE in your response so I can see where you are having trouble.
Thanks.