How to change background color of JTabbedPane?
Using TabComponentsDemo
as an example, setBackgroundAt()
seems to work:
private void initTabComponent(int i) {
pane.setTabComponentAt(i, new ButtonTabComponent(pane));
pane.setBackgroundAt(i, Color.getHSBColor((float)i/tabNumber, 1, 1));
}
Addendum: As @camickr helpfully observed, 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 http://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, 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();
}
});
}
}
Set the Background Color for JTabbedPane
There are a few different things you can do, depending upon how much control you want over the exact color. The easiest way is to change some of the properties in the UIManager to change the colors that Nimbus derives its other colors from. I played around a little with your code, and found that if I put the following code after the call to UIManager.setLookAndFeel(), it would approximate the red-on-black look you attempted in your example:
UIManager.put("nimbusBase", new ColorUIResource(0, 0, 0));
UIManager.put("textForeground", new ColorUIResource(255, 0, 0));
I'll leave it to you to experiment. For more information to experiment with, there is a good article on configuring Nimbus here. Be sure you look at his link titled "Nimbus UIDefaults Properties List". Outside of just massaging the colors to something similar to what you want, you'll have to start doing messy things like implementing Painter classes that do custom painting.
Colorize a tab in a JTabbedPane using java swing
most of method for
JTabbedPane
is protected in the API, and not accesible from Swing methodshave to look for
Custom XxxTabbedPaneUI
, override these methods, and could be accesible from outsidecorrect way would be to implement Custom Look & Feel only, part of them override
JTabbedPane
example for Custom XxxTabbedPaneUI
Change the background color behind the tabs of a JTabbedPane?
OK, I figured it out. You need to specify your own custom UI.
- You need to extend the
BasicTabbedPaneUI
class and overridepaint
. - Then you can set the UI:
tabbedPane.setUI(new CustomTabbedUI(Color.RED));
This really old answer I dug up was partially correct. Obviously it was incomplete.
Launcher.java
package q60855752;
import javax.swing.*;
public class Launcher {
public static void main(String[] args) {
SwingUtilities.invokeLater(new App());
}
}
App.java
package q60855752;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
public class App extends JPanel implements Runnable {
public App() {
super(new BorderLayout());
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.setUI(new CustomTabbedUI(Color.RED));
// Modified: https://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html
addPanel(tabbedPane, "Tab 1", "Panel #1", "Does nothing", KeyEvent.VK_1, null);
addPanel(tabbedPane, "Tab 2", "Panel #2", "Does twice as much nothing", KeyEvent.VK_2, null);
addPanel(tabbedPane, "Tab 3", "Panel #3", "Still does nothing", KeyEvent.VK_3, null);
addPanel(tabbedPane, "Tab 4", "Panel #4", "Panel #4 (has a preferred size of 410 x 50).", KeyEvent.VK_4, new Dimension(410, 50));
add(tabbedPane, BorderLayout.CENTER);
}
@Override
public void run() {
JFrame frame = new JFrame("App");
frame.setPreferredSize(new Dimension(500, 200));
frame.setContentPane(new App());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
protected void addPanel(JTabbedPane tabbedPane, String title, String content, String tooltip, int mnemonic, Dimension dimensions) {
JComponent panel = createPanel(title);
if (dimensions != null) panel.setPreferredSize(dimensions);
tabbedPane.addTab(content, null, panel, tooltip);
tabbedPane.setMnemonicAt(tabbedPane.getTabCount() - 1, mnemonic);
}
protected JComponent createPanel(String text) {
JPanel panel = new JPanel(false);
JLabel filler = new JLabel(text);
filler.setHorizontalAlignment(JLabel.CENTER);
panel.setLayout(new GridLayout(1, 1));
panel.add(filler);
return panel;
}
}
CustomTabbedUI.java
package q60855752;
import javax.swing.*;
import javax.swing.plaf.basic.BasicTabbedPaneUI;
import java.awt.*;
public class CustomTabbedUI extends BasicTabbedPaneUI {
private Color backgroundColor;
public CustomTabbedUI(Color backgroundColor) {
super();
this.backgroundColor = backgroundColor;
}
public void setBackgroundColor(Color backgroundColor) {
this.backgroundColor = backgroundColor;
}
@Override
public void paint(Graphics g, JComponent c) {
Rectangle bounds = tabPane.getBounds();
g.setColor(this.backgroundColor);
g.fillRect(0, 0, bounds.width, bounds.height);
super.paint(g, c); // Call parent...
}
}
How can i change jtabbedpane selected tab background color
Call this UIManager.put("TabbedPane.selected", Color.Red);
before initComponents();
JTabbedPanel change selected tab background color
This is worked for me
UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab[Enabled].backgroundPainter", new BackgroundPainter(Color.white));
UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab[Enabled+MouseOver].backgroundPainter", new BackgroundPainter(Color.white));
UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab[Enabled+Pressed].backgroundPainter", new BackgroundPainter(Color.white));
UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab[Focused+MouseOver+Selected].backgroundPainter", new BackgroundPainter(Color.white));
UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab[Focused+Pressed+Selected].backgroundPainter", new BackgroundPainter(Color.white));
UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab[Focused+Selected].backgroundPainter", new BackgroundPainter(Color.GRAY));
UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab[MouseOver+Selected].backgroundPainter", new BackgroundPainter(Color.white));
UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab[Pressed+Selected].backgroundPainter", new BackgroundPainter(Color.white));
UIManager.getLookAndFeelDefaults().put("TabbedPane:TabbedPaneTab[Selected].backgroundPainter", new BackgroundPainter(Color.white));
BackgroundPainter class
public class BackgroundPainter implements Painter<JComponent> {
private Color color = null;
BackgroundPainter(Color c) {
color = c;
}
@Override
public void paint(Graphics2D g, JComponent object, int width, int height) {
if (color != null) {
g.setColor(color);
g.fillRect(0, 0, width - 1, height - 1);
}
}
}
Change Color of JTabbedPanel side view
The basic logic would be:
JPanel tab1 = new JPanel();
tab1.background( Color.RED );
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.add(tab1, "Tab1");
JPanel parent = new JPanel( new BorderLayout() );
parent.setBackground( Color.GREEN );
parent.add( tabbedPane );
frame.add( parent );
Now the area occupied by the tab will have the background color of the "parent" panel.
If this doesn't work, then you have a Look and Feel issue.
Solution
the issue was resolved after changing look and feel
Related Topics
Javafx Exception in Thread "Main" Java.Lang.Noclassdeffounderror: Javafx/Application/Application
Find an Array Inside Another Larger Array
How to Get Which Jradiobutton Is Selected from a Buttongroup
Why I'm Not Able to Unwrap and Serialize a Java Map Using the Jackson Java Library
How to Intercept a Method Invocation with Standard Java Features (No Aspectj etc)
What Is the Jvm Thread Scheduling Algorithm
Performance Difference Between Java 8 Lambdas and Anonymous Inner Classes
Interpreting Jemaloc Data Possible Off-Heap Leak
How to Emit and Handle Custom Events
How to Achieve Transfer File Between Client and Server Using Java Socket
How to Draw a Decent Looking Circle in Java
Gson.Tojson() Throws Stackoverflowerror
How Do Format a Phone Number as a String in Java