Swing/ClippedTitleTab のバックアップの現在との差分(No.12)
TITLE:JTabbedPaneのタブを等幅にしてタイトルをクリップ
JTabbedPaneのタブを等幅にしてタイトルをクリップ
編集者:Terai Atsuhiro~
作成日:2005-08-08
更新日:2022-12-16 (金) 13:56:00
概要
JTabbedPane
のタブを等幅にし、長いタイトルはクリップして表示します。
Screenshot
Advertisement
概要
JTabbedPaneのタブを等幅にし、長いタイトルはクリップして表示します。#screenshot
サンプルコード
#spanend
#spanadd
* サンプルコード [#sourcecode]
#spanend
#spanadd
#code(link){{
#spanend
final Insets tabInsets = UIManager.getInsets("TabbedPane.tabInsets");
#spandel
tab1.setUI(new javax.swing.plaf.basic.BasicTabbedPaneUI() {
#spanend
@Override
protected int calculateTabWidth(int tabPlacement, int tabIndex, FontMetrics metrics) {
int width = tabPane.getWidth() - 4; //?4?
switch(tabPlacement) {
#spanadd
tab1.setUI(new BasicTabbedPaneUI() {
#spanend
@Override protected int calculateTabWidth(
int tabPlacement, int tabIndex, FontMetrics metrics) {
Insets insets = tabPane.getInsets();
Insets tabAreaInsets = getTabAreaInsets(tabPlacement);
int width = tabPane.getWidth() - insets.left - insets.right
- tabAreaInsets.left - tabAreaInsets.right;
switch (tabPlacement) {
case LEFT: case RIGHT:
return (int)(width/4);
return (int) (width / 4);
case BOTTOM: case TOP: default:
return (int)(width/tabPane.getTabCount());
return (int) (width / tabPane.getTabCount());
}
}
@Override
protected void paintText(Graphics g, int tabPlacement,
Font font, FontMetrics metrics, int tabIndex,
String title, Rectangle textRect,
boolean isSelected) {
#spanadd
#spanend
@Override protected void paintText(
Graphics g, int tabPlacement, Font font, FontMetrics metrics,
int tabIndex, String title, Rectangle textRect, boolean isSelected) {
Rectangle tabRect = rects[tabIndex];
Rectangle rect = new Rectangle(textRect.x+tabInsets.left, textRect.y,
tabRect.width-tabInsets.left-tabInsets.right, textRect.height);
String clippedText = SwingUtilities.layoutCompoundLabel(metrics, title, null,
SwingUtilities.CENTER, SwingUtilities.CENTER,
SwingUtilities.CENTER, SwingUtilities.TRAILING,
rect, new Rectangle(), rect, 0);
if(title.equals(clippedText)) {
Rectangle rect = new Rectangle(textRect.x + tabInsets.left, textRect.y,
tabRect.width - tabInsets.left - tabInsets.right, textRect.height);
String clippedText = SwingUtilities.layoutCompoundLabel(
metrics, title, null,
SwingUtilities.CENTER, SwingUtilities.CENTER,
SwingUtilities.CENTER, SwingUtilities.TRAILING,
rect, new Rectangle(), rect, 0);
if (title.equals(clippedText)) {
super.paintText(
g, tabPlacement, font, metrics, tabIndex, title, textRect, isSelected);
} else {
rect = new Rectangle(textRect.x + tabInsets.left, textRect.y,
tabRect.width - tabInsets.left - tabInsets.right, textRect.height);
super.paintText(g, tabPlacement, font, metrics, tabIndex,
title, textRect, isSelected);
}else{
rect = new Rectangle(textRect.x+tabInsets.left, textRect.y,
tabRect.width-tabInsets.left-tabInsets.right, textRect.height);
super.paintText(g, tabPlacement, font, metrics, tabIndex,
clippedText, rect, isSelected);
}
}
});
- &jnlp;
- &jar;
- &zip;
解説
上記のサンプルでは、BasicTabbedPaneUI#calculateTabWidthメソッドをオーバーライドして、JTabbedPaneのタブ幅が、すべて等しくなるように設定しています。解説
上記のサンプルでは、BasicTabbedPaneUI#calculateTabWidth(...)
メソッドをオーバーライドして、JTabbedPane
のタブ幅がすべて等しくなるように設定しています。
タイトル文字列のほうが、このタブ幅より長い場合は、SwingUtilities.layoutCompoundLabelメソッドで文字列をクリップして表示します。
- タイトル文字列のほうがこのタブ幅より長い場合は
SwingUtilities.layoutCompoundLabel(...)
メソッドで文字列をクリップして表示 - タイトルがクリップされていてもツールチップで元の文字列を表示する
- タブの位置を左右にした場合このサンプルでは全体の幅の
1/4
のタブ幅になるように設定
#screenshot(,screenshot1.png)
-
Java 1.6.0
以上の場合JTabbedPane#setTabComponentAt(...)
メソッドでタブにJLabel
を使用してそのデフォルトのクリップ機能が利用可能
#spanend
#spandel
class MyJTabbedPane extends JTabbedPane {
#spanend
private final Insets tabInsets = UIManager.getInsets("TabbedPane.tabInsets");
public MyJTabbedPane() {
super();
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
MyJTabbedPane c = (MyJTabbedPane)e.getSource();
c.initTabWidth();
}
});
addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
MyJTabbedPane c = (MyJTabbedPane)e.getSource();
c.initTabWidth();
}
});
}
public void addTab(String title, final JComponent content) {
super.addTab(null, content);
JLabel label = new JLabel(title, JLabel.CENTER);
Dimension dim = label.getPreferredSize();
label.setPreferredSize(new Dimension(0, dim.height+tabInsets.top+tabInsets.bottom));
setTabComponentAt(getTabCount()-1, label);
setToolTipTextAt(getTabCount()-1, title);
initTabWidth();
}
private void initTabWidth() {
int paneWidth = getWidth() - 4; //?4?
int tabCount = getTabCount();
int tabPlacement = getTabPlacement();
int tabWidth = tabInsets.left + tabInsets.right + 3;
switch(tabPlacement) {
case LEFT: case RIGHT:
tabWidth = (int)(paneWidth/4) - tabWidth;
break;
case BOTTOM: case TOP: default:
tabWidth = (int)(paneWidth/tabCount) - tabWidth;
}
for(int i=0;i<tabCount;i++) {
JLabel l = (JLabel)getTabComponentAt(i);
if(l!=null) {
Dimension dim = l.getPreferredSize();
l.setPreferredSize(new Dimension(tabWidth, dim.height));
}
}
revalidate();
}
#spandel
}
#spanend
#spandel