How do I change the background color of jtabbedpane?
•
Java
I know you can modify the LAF properties, but if you don't, how can you do it? I'm just asking, because setbackground doesn't seem to do that
Please note that I am looking to change the following properties:
> TabbedPane. Background (or tabbedpane. Contentareacolor?) > TabbedPane. tabAreaBackground
Solution
Taking tabcomponentsdemo as an example, setbackgroundat() seems to be valid:
private void initTabComponent(int i) {
pane.setTabComponentAt(i,new ButtonTabComponent(pane));
pane.setBackgroundAt(i,Color.getHSBColor((float)i/tabNumber,1,1));
}
Appendix: as @ camickr does, the target component must be opaque
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
/** @see https://stackoverflow.com/questions/8752037 */
public class TabColors extends JPanel {
private static final int MAX = 5;
private final JTabbedPane pane = new JTabbedPane();
public TabColors() {
for (int i = 0; i < MAX; i++) {
Color color = Color.getHSBColor((float) i / MAX,1);
pane.add("Tab " + String.valueOf(i),new TabContent(i,color));
pane.setBackgroundAt(i,color);
}
this.add(pane);
}
private static class TabContent extends JPanel {
private TabContent(int i,Color color) {
setOpaque(true);
setBackground(color);
add(new JLabel("Tab content " + String.valueOf(i)));
}
@Override
public Dimension getPreferredSize() {
return new Dimension(320,240);
}
}
private void display() {
JFrame f = new JFrame("TabColors");
f.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokelater(new Runnable() {
@Override
public void run() {
new TabColors().display();
}
});
}
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
