Java Swing How to - Place text (image) in a tab in JTabbedPane with JLabel








Question

We would like to know how to place text (image) in a tab in JTabbedPane with JLabel.

Answer

import java.awt.BorderLayout;
//ww w  .  j av  a 2 s .c o m
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

public class Main {
  public static void main(String[] args) {
    JTabbedPane tp = new JTabbedPane();
    tp.addTab("Dates", new JPanel());
    tp.addTab("Deliveries", new JPanel());
    tp.addTab("Exports", new JPanel());

    tp.setTabComponentAt(0, new JLabel("Dates"));
    tp.setTabComponentAt(1, new JLabel("Deliveries"));
    tp.setTabComponentAt(2, new JLabel("Exports"));

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