3

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

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Abin
  • 540
  • 4
  • 15

2 Answers2

3

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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(makeUI());
    f.setSize(320, 240);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowUI();
      }
    });
  }
}
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);
  }
}
aterai
  • 9,658
  • 4
  • 35
  • 44
  • Great !! Working...Thanks a lot – Abin Mar 18 '13 at 11:30
  • Can u please tell me how to add a JPopupMenu to this label ("A Label at right corner") to add some menu items on this label. pleaseee – Abin Apr 05 '13 at 09:51
  • I guess you might want to override `LayerUI#processMouseEvent(MouseEvent e, JLayer l)` and do it like this: `if(labelRect.contains(pt) && e.isPopupTrigger()) popup.show(tabbedPane, pt.x, pt.y);` – aterai Apr 06 '13 at 10:34
2

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.. If any one have the solution, please help me.

You can use:

  1. JLayer (Java7) based on JXLayer(Java6)

  2. GlassPane, for example


I'd use JLayer if is possible (requires Java7).

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319