title: Change JTabbedPane active tab color dynamically
body: <p>My requirement is to change tabs colos of JTabbedPane dynamically.  Like change of tabs and selected tabs colors on button click.  For the same reason I cannot use UIManager.  But tabs color once set and changing tab is not working properly. Also selected tab is always showing default color and not the one I set.</p>
  <p>Below is the code:</p>
  <pre><code>import javax.swing.*;
  import java.awt.*;
  
  public class TestJTP extends JFrame {
  
      JTabbedPane tabbedPane;
      public TestJTP() {
          init();
      }
  
      private void init() {
  
          tabbedPane = new JTabbedPane();
          tabbedPane.addTab(&quot;Tab-1&quot;, null);
          tabbedPane.addTab(&quot;Tab-2&quot;, null);
          tabbedPane.addTab(&quot;Tab-3&quot;, null);
          tabbedPane.addTab(&quot;Tab-4&quot;, null);
          tabbedPane.addTab(&quot;Tab-5&quot;, null);
  
          JPanel panel = new JPanel();
          JButton b1 = new JButton(&quot;Orange,blue&quot;);
          b1.addActionListener(e -&gt; {
              tabbedPane.setBackground(null);
              tabbedPane.setBackgroundAt(tabbedPane.getSelectedIndex(), null);
              tabbedPane.setBackground(Color.orange);
              tabbedPane.setBackgroundAt(tabbedPane.getSelectedIndex(), Color.blue);
          });
          JButton b2 = new JButton(&quot;Red,cyan&quot;);
          b2.addActionListener(e -&gt; {
              tabbedPane.setBackground(null);
              tabbedPane.setBackground(Color.red);
              tabbedPane.setBackgroundAt(tabbedPane.getSelectedIndex(), null);
              tabbedPane.setBackgroundAt(tabbedPane.getSelectedIndex(), Color.cyan);
          });
          JButton b3 = new JButton(&quot;Green,magenta&quot;);
          b3.addActionListener(e -&gt; {
              tabbedPane.setBackground(null);
              tabbedPane.setBackground(Color.green);
              tabbedPane.setBackgroundAt(tabbedPane.getSelectedIndex(), null);
              tabbedPane.setBackgroundAt(tabbedPane.getSelectedIndex(), Color.magenta);
          });
          panel.add(b1);
          panel.add(b2);
          panel.add(b3);
          getContentPane().add(panel, BorderLayout.NORTH);
          getContentPane().add(tabbedPane, BorderLayout.CENTER);
          pack();
          setVisible(true);
          setExtendedState(JFrame.MAXIMIZED_BOTH);
      }
  
      public static void main(String[] args) {
          new TestJTP();
      }
  }
  </code></pre>