How to change background color of JTabbedPane?

I know that you can change the properties of LaF , but how do you do this without doing this? I only ask because setBackground does not seem to do this.

Please note that I want to change the following properties:

  • TabbedPane.background (or TabbedPane.contentAreaColor ?)
  • TabbedPane.tabAreaBackground
+6
source share
2 answers

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 observed, the target component should be opaque .

Tabcolors

 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(); } }); } } 
+17
source

You can also do the following:

 for (int i = 0; i < this.getTabCount(); i++) { this.getComponentAt(i).setBackground(Color.DARK_GRAY); } 

or

 for (int i = 0; i < this.getTabCount(); i++) { this.setBackgroundAt(i, Color.DARK_GRAY); this.getComponentAt(i).setBackground(Color.DARK_GRAY); } 

for background tabs and panels

0
source

Source: https://habr.com/ru/post/905266/


All Articles