0

I am trying to add an image to a tab so it looks like an icon.I want to put a png image on the tab (check image)they are those 4 tabs within a tabbed pane
Is it possible to do this in java?

  • hmm ... don't see an icon/image in the screenshot - why doesn't tabbedPane api for setting an icon suit your needs? – kleopatra Jul 15 '13 at 09:12

2 Answers2

6

JTabbedPane allows you to provide a component to act as the tab "renderer" (of sorts).

Take a look at JTabbedPane#setTabComponentAt for more details and check out this example for more details.

Updated with example

enter image description here

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestTabbedPaneIcon {

    public static void main(String[] args) {
        new TestTabbedPaneIcon();
    }

    public TestTabbedPaneIcon() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JTabbedPane tp = new JTabbedPane();
                tp.addTab("Dates", new JPanel());
                tp.addTab("Deliveries", new JPanel());
                tp.addTab("Exports", new JPanel());

                tp.setTabComponentAt(0, getLabel("Dates", "/Icon03.png"));
                tp.setTabComponentAt(1, getLabel("Deliveries", "/Icon01.png"));
                tp.setTabComponentAt(2, getLabel("Exports", "/Icon02.png"));

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(tp);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    protected JLabel getLabel(String title, String icon) {
        JLabel label = new JLabel(title);
        try {
            label.setIcon(new ImageIcon(ImageIO.read(getClass().getResource(icon))));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return label;
    }
}
Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
2

JTabbedPane has api to set an icon to the tab, either when adding the tab content or later:

// when adding
tabbedPane.addTab(String, Icon, Component);
// after having added
tabbedPane.setIconAt(int, Icon);
kleopatra
  • 51,061
  • 28
  • 99
  • 211
  • +1 but note once time that I tried I had an issue with protected Dimension available by SwingUtilities.layoutLabel()???, even Icon had proper H&W in pixels – mKorbel Jul 15 '13 at 09:41