3

I am preparing a window with some horizontal tabs using JTabbedPane, Tabs And window are prepared properly.

Now I need to setup these tabs in level basis, based on my requirement (My logic returns integer value based on that I need to setup levels ).

Levels look like :

enter image description here

Can you please advise?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Giri
  • 507
  • 6
  • 23
  • 2
    Not really supported, the maximum control can be achieved by tabbedPane.setTabComponentAt(..) it allows to use custom "tabs" (but without layout control) – kleopatra Aug 05 '13 at 09:23
  • 1
    Can you use an icon, for [example](http://stackoverflow.com/a/3484251/230513)? – trashgod Aug 05 '13 at 11:28

2 Answers2

7

Just the sight of screenshot:

enter image description here

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TabHeightTest {
  public JComponent makeUI() {
    JTabbedPane tabbedPane = new JTabbedPane(
      JTabbedPane.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
    tabbedPane.setUI(new javax.swing.plaf.basic.BasicTabbedPaneUI() {
      @Override protected int calculateTabHeight(
        int tabPlacement, int tabIndex, int fontHeight) {
        return 32;
      }
      @Override protected void paintTab(
        Graphics g, int tabPlacement, Rectangle[] rects, int tabIndex,
        Rectangle iconRect, Rectangle textRect) {
        if(tabIndex==0) {
          rects[tabIndex].height = 20 + 1;
          rects[tabIndex].y = 32 - rects[tabIndex].height + 1;
        } else if(tabIndex==1) {
          rects[tabIndex].height = 26 + 1;
          rects[tabIndex].y = 32 - rects[tabIndex].height + 1;
        }
        super.paintTab(g, tabPlacement, rects, tabIndex, iconRect, textRect);
      }
    });
    tabbedPane.addTab("000", new JLabel("aaaaaaaaaaa"));
    tabbedPane.addTab("111", new JScrollPane(new JTree()));
    tabbedPane.addTab("222", new JSplitPane());

    return tabbedPane;
  }
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.getContentPane().add(new TabHeightTest().makeUI());
    frame.setSize(320, 240);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}
aterai
  • 9,658
  • 4
  • 35
  • 44
1

The tab bounds are the responsibility of the look and feel. If you are in control of that (and if you're not, you should not try such non-portable tricks anyway), you can modify it in the layout manager of the tabbed pane.

For BasicLookAndFeel the tab bounds calculation is done in BasicTabbedPaneUI.TabbedPaneLayout.calculateTabRects() and BasicTabbedPaneUI.TabbedPaneScrollLayout.calculateTabRects(). For Basic L&F derived themes, BasicTabbedPaneUI has a createLayout() method which can be overridden to return the layout manager(s) with the desired behaviour.

kiheru
  • 6,588
  • 25
  • 31