How do I add icon to JTabbedPane tabs?

The following code snippet demonstrates how to create a JTabbedPane component with image icons attached on their tabs. There are two steps that we need to do to achieve this. First we need to load the image icon and after that we need to attach these icons to the tabs.

To create or load an image icon simply create an instance of ImageIcon class. Pass the information about the image location to the constructor of the ImageIcon. In the example below I provide the location of the image using the getClass().getResource() method which will load the images from resources directory.

To add a new tab with image icon attached to the tabs of a JTabbedPane component we are using the addTab(String, Icon, Component) method. The second argument in this method is the image icon of the tab.

Let’s see the code snippet below:

package org.kodejava.swing;

import javax.swing.*;
import java.awt.*;
import java.util.Objects;

public class TabbedPaneWithIcon extends JPanel {
    private TabbedPaneWithIcon() {
        initializeUI();
    }

    private static void showFrame() {
        JPanel panel = new TabbedPaneWithIcon();
        panel.setOpaque(true);

        JFrame frame = new JFrame("Tabbed Pane With Icon Demo");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setContentPane(panel);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(TabbedPaneWithIcon::showFrame);
    }

    private void initializeUI() {
        this.setLayout(new BorderLayout());
        this.setPreferredSize(new Dimension(500, 200));

        JTabbedPane pane = new JTabbedPane();

        ImageIcon tab1Icon = new ImageIcon(Objects.requireNonNull(
                this.getClass().getResource("/images/test-pass-icon.png")));
        ImageIcon tab2Icon = new ImageIcon(Objects.requireNonNull(
                this.getClass().getResource("/images/test-fail-icon.png")));
        ImageIcon tab3Icon = new ImageIcon(Objects.requireNonNull(
                this.getClass().getResource("/images/test-error-icon.png")));

        JPanel content1 = new JPanel();
        JPanel content2 = new JPanel();
        JPanel content3 = new JPanel();

        pane.addTab("Pass", tab1Icon, content1);
        pane.addTab("Fail", tab2Icon, content2);
        pane.addTab("Error", tab3Icon, content3);

        this.add(pane, BorderLayout.CENTER);
    }
}

The directory structure of the project:

kodejava-swing
├─ pom.xml
└─ src
   └─ main
      ├─ java
      │  └─ org
      │     └─ kodejava
      │        └─ swing
      │           └─ TabbedPaneWithIcon.java
      └─ resources
         └─ images
            ├─ test-error-icon.png
            ├─ test-fail-icon.png
            └─ test-pass-icon.png

And here is the result of the code snippet above.

JTabbedPane Tab with Icon Demo

Wayan

2 Comments

  1. You did not include your packages and folders I am getting a huge roster of errors and I just copied and pasted your code.

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at javax.swing.ImageIcon.(ImageIcon.java:217)
        at tabbedpanewithicon.TabbedPaneWithIcon.initializeUI(TabbedPaneWithIcon.java:37)
        at tabbedpanewithicon.TabbedPaneWithIcon.(TabbedPaneWithIcon.java:8)
        at tabbedpanewithicon.TabbedPaneWithIcon.showFrame(TabbedPaneWithIcon.java:12)
        at tabbedpanewithicon.TabbedPaneWithIcon$1.run(TabbedPaneWithIcon.java:25)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
        at java.awt.EventQueue.access$500(EventQueue.java:97)
        at java.awt.EventQueue$3.run(EventQueue.java:709)
        at java.awt.EventQueue$3.run(EventQueue.java:703)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
    BUILD SUCCESSFUL (total time: 1 second)
    
    Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.