Colorize a tab in a JTabbedPane using java swing

痴心易碎 提交于 2019-12-01 10:57:34
mKorbel
  • most of method for JTabbedPane is protected in the API, and not accesible from Swing methods

  • have to look for Custom XxxTabbedPaneUI, override these methods, and could be accesible from outside

  • correct way would be to implement Custom Look & Feel only, part of them override JTabbedPane

  • example for Custom XxxTabbedPaneUI

You can change the background color of the tab using setBackgroundAt(), as shown here.

You can change the background color of the tab's content using setBackground(), as shown here. Typically you have to do this on the tab's content, as the enclosing JTabbedPane background color is obscured by the content.

If you still have trouble, please edit your question to include an sscce based on either example that exhibits the problem you envounter.

Addendum: Combining the methods is also possible:

import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;

public class JTabbedTest {

    private static JTabbedPane jtp;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                jtp = new JTabbedPane();
                jtp.setPreferredSize(new Dimension(320, 200));
                jtp.addTab("Reds", new ColorPanel(0, Color.RED));
                jtp.setBackgroundAt(0, Color.RED);
                jtp.addTab("Greens", new ColorPanel(1, Color.GREEN));
                jtp.setBackgroundAt(1, Color.GREEN);
                jtp.addTab("Blues", new ColorPanel(2, Color.BLUE));
                jtp.setBackgroundAt(2, Color.BLUE);

                f.add(jtp, BorderLayout.CENTER);
                f.pack();
                f.setVisible(true);
            }
        });
    }

    private static class ColorPanel extends JPanel implements ActionListener {

        private final Random rnd = new Random();
        private final Timer timer = new Timer(1000, this);
        private Color color;
        private Color original;
        private int mask;
        private JLabel label = new JLabel("Stackoverflow!");
        private int index;

        public ColorPanel(int index, Color color) {
            super(true);
            this.color = color;
            this.original = color;
            this.mask = color.getRGB();
            this.index = index;
            this.setBackground(color);
            label.setForeground(color);
            this.add(label);
            timer.start();
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            color = new Color(rnd.nextInt() & mask);
            this.setBackground(color);
            jtp.setBackgroundAt(index, original);
        }
    }
}

You should consider using a Look and Feel that does what you want, or failing that, changing the default UIManger settings for a JTabbedPane:

UIManager.put("TabbedPane.background", Color.GRAY);

If you opt for the latter, it must be done before you create your GUI.

For more on this, please see Rob Camick's excellent blog on the subject: UIManager Defaults.

Edit: I was wrong. It should be:

UIManager.put("TabbedPane.unselectedBackground", Color.GRAY);

But note that this may not work with every Look and Feel.

DrEnquinox

It may be a problem that there is nothing added yet to the tab.

Try setting the content manager of the content panel to BorderLayout, adding a JPanel with BorderLayout. Center and then coloring that panel.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!