http://java-swing-tips.blogspot.com/2008/03/horizontally-fill-tab-of-jtabbedpane.htmlclass ClippedTitleTabbedPane extends JTabbedPane { private final Insets tabInsets = UIManager.getInsets("TabbedPane.tabInsets"); private final Insets tabAreaInsets = UIManager.getInsets("TabbedPane.tabAreaInsets"); public ClippedTitleTabbedPane() { super(); addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { initTabWidth(); } }); } @Override public void insertTab(String title, Icon icon, Component component, String tip, int index) { super.insertTab(title, icon, component, tip==null?title:tip, index); JLabel label = new JLabel(title, JLabel.CENTER); Dimension dim = label.getPreferredSize(); label.setPreferredSize(new Dimension(0, dim.height+tabInsets.top+tabInsets.bottom)); setTabComponentAt(index, label); initTabWidth(); } public void initTabWidth() { Insets insets = getInsets(); int areaWidth = getWidth() - tabAreaInsets.left - tabAreaInsets.right - insets.left - insets.right; int tabCount = getTabCount(); int tabWidth = tabInsets.left + tabInsets.right + 3; switch(getTabPlacement()) { case LEFT: case RIGHT: tabWidth = (int)(areaWidth/4) - tabWidth; break; case BOTTOM: case TOP: default: tabWidth = (int)(areaWidth/tabCount) - tabWidth; } for(int i=0;i < tabCount;i++) { JLabel l = (JLabel)getTabComponentAt(i); if(l==null) break; l.setPreferredSize(new Dimension(tabWidth, l.getPreferredSize().height)); } } @Override public synchronized void repaint() { initTabWidth(); super.repaint(); } }
http://blog.elevenworks.com/?p=4Step 7: Specify the size of the tabsIn order to specify the size of the tabs we need to override the calculateTabHeight method and the calculateTabWidth method. We can also use the calculateTabHeight method to enforce the fact that a tab will always be a height that is divisible by two. This will make sure that the angled line on the right hand side of the tabs always looks good (unlike the screenshots from the previous step). protected int calculateTabHeight(int tabPlacement, int tabIndex, int fontHeight) { int vHeight = fontHeight; if (vHeight % 2 > 0) { vHeight += 1; } return vHeight; } protected int calculateTabWidth(int tabPlacement, int tabIndex, FontMetrics metrics) { return super.calculateTabWidth(tabPlacement, tabIndex, metrics) + metrics.getHeight(); }
private final Insets tabInsets = UIManager.getInsets("TabbedPane.tabInsets");
private final Insets tabAreaInsets = UIManager.getInsets("TabbedPane.tabAreaInsets");
public ClippedTitleTabbedPane() {
super();
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
initTabWidth();
}
});
}
@Override
public void insertTab(String title, Icon icon, Component component, String tip, int index) {
super.insertTab(title, icon, component, tip==null?title:tip, index);
JLabel label = new JLabel(title, JLabel.CENTER);
Dimension dim = label.getPreferredSize();
label.setPreferredSize(new Dimension(0, dim.height+tabInsets.top+tabInsets.bottom));
setTabComponentAt(index, label);
initTabWidth();
}
public void initTabWidth() {
Insets insets = getInsets();
int areaWidth = getWidth() - tabAreaInsets.left - tabAreaInsets.right
- insets.left - insets.right;
int tabCount = getTabCount();
int tabWidth = tabInsets.left + tabInsets.right + 3;
switch(getTabPlacement()) {
case LEFT: case RIGHT:
tabWidth = (int)(areaWidth/4) - tabWidth;
break;
case BOTTOM: case TOP: default:
tabWidth = (int)(areaWidth/tabCount) - tabWidth;
}
for(int i=0;i < tabCount;i++) {
JLabel l = (JLabel)getTabComponentAt(i);
if(l==null) break;
l.setPreferredSize(new Dimension(tabWidth, l.getPreferredSize().height));
}
}
@Override
public synchronized void repaint() {
initTabWidth();
super.repaint();
}
}
{
int vHeight = fontHeight;
if (vHeight % 2 > 0)
{
vHeight += 1;
}
return vHeight;
} protected int calculateTabWidth(int tabPlacement, int tabIndex, FontMetrics metrics)
{
return super.calculateTabWidth(tabPlacement, tabIndex, metrics) + metrics.getHeight();
}
ALMModelObject theWorkspaceModel = ALMApplication.message_getWorkspace();
ALMController theWorkspaceController = theWorkspaceModel.newController();
theApplicationController.registerFolder(CoreConstants.keyResultFolders, theWorkspaceController);
ALMTreeBuilderNode theTreeBuilder = new ALMTreeBuilderNode(theWorkspaceController, ALMControllerTreeNodeFactory.sFactory);
theTreeBuilder.setRecursiveBuild(true);
DefaultNamedObject theTabDelegate = new DefaultNamedObject(almLocalized("lbl_resultfolders"));
ImageIcon theIcon = getBundle().getImageIcon("icon-resultfolders.png");
if (theIcon != null)
theTabDelegate.setIcon(theIcon);
theApplicationController.registerTabView(1000, CoreConstants.keyResultFolders,theTabDelegate, theTreeBuilder);
}