Abin Abin - 11 months ago 47
Java Question

Tabed pane with a label on top

I need to add a JLabel/JPanel as shown in the image. When resizing the frame both the tabbed pane and the Label/panel should be resided. The label and the panel inside the Tabbed Pane are independent. How can I do that?

enter image description here


Here is a quick example to use JLayer(as suggested by mKorbel):

import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.*;

public class TopRightCornerLabelLayerUITest {
  public static JComponent makeUI() {
    JTabbedPane tab = new JTabbedPane();
    tab.addTab("New tab1", new JLabel("1"));
    tab.addTab("New Tab2", new JLabel("2"));
    JPanel p = new JPanel(new BorderLayout());
    p.add(new JLayer<JComponent>(tab, new TopRightCornerLabelLayerUI()));
    return p;
  private static void createAndShowUI() {
    JFrame f = new JFrame();
    f.setSize(320, 240);
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
class TopRightCornerLabelLayerUI extends LayerUI<JComponent> {
  private JLabel l = new JLabel("A Label at right corner");
  private JPanel rubberStamp = new JPanel();
  @Override public void paint(Graphics g, JComponent c) {
    super.paint(g, c);
    Dimension d = l.getPreferredSize();
    int x = c.getWidth() - d.width - 5;
    SwingUtilities.paintComponent(g, l, rubberStamp, x, 2, d.width, d.height);