Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Using <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html#eg" rel="noreferrer"><code>TabComponentsDemo</code></a> as an example, <code>setBackgroundAt()</code> seems to work: </p> <pre><code>private void initTabComponent(int i) { pane.setTabComponentAt(i, new ButtonTabComponent(pane)); pane.setBackgroundAt(i, Color.getHSBColor((float)i/tabNumber, 1, 1)); } </code></pre> <p>Addendum: As @camickr helpfully observed, the target component must be <a href="http://java.sun.com/products/jfc/tsc/articles/painting/index.html#props" rel="noreferrer">opaque</a>.</p> <p><img src="https://i.stack.imgur.com/sNVWR.png" alt="TabColors"></p> <pre><code>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 &lt; 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(); } }); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload