Friday, November 16, 2007

Giant Tabs in a JTabbedPane

Here's how to create a Java Swing JTabbedPane with over-sized tabs. The most basic method is to extend JTabbedPane with a new class that replaces the tabbed pane UI. The replacement UI in turn can override the methods that control the tabs size. The font used in the tabs can also be replaced in the new tabbed pane constructor.

Here's a tabbed pane class with over-sized tabs (50 tall and 140 wide).



public class GiantTabbedPane extends JTabbedPane {
public GiantTabbedPane() {
setUI((GiantTabbedPaneUI)GiantTabbedPaneUI.createUI(this));
setFont(new java.awt.Font("Arial Unicode MS", 1, 18));
}

static class GiantTabbedPaneUI extends BasicTabbedPaneUI {
public static ComponentUI createUI(JComponent c) {
return new GiantTabbedPaneUI();
}

protected int calculateTabWidth(int tabPlacement,
int tabIndex,
FontMetrics metrics) {
return 140;
}

protected int calculateTabHeight(int tabPlacement,
int tabIndex,
int fontHeight) {
return 50;
}
}
}

No comments:

Post a Comment