Swing/ClippedTitleTab のバックアップの現在との差分(No.12)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/ClippedTitleTab へ行く。
- 1 (2005-10-20 (木) 14:50:04)
- 2 (2005-11-06 (日) 22:06:26)
- 3 (2006-02-27 (月) 15:33:21)
- 4 (2006-03-31 (金) 16:41:01)
- 5 (2006-04-12 (水) 19:37:18)
- 6 (2006-05-06 (土) 01:49:21)
- 7 (2007-05-29 (火) 11:54:54)
- 8 (2007-09-26 (水) 20:12:54)
- 9 (2007-09-26 (水) 21:26:03)
- 10 (2007-09-27 (木) 01:18:08)
- 11 (2007-09-27 (木) 15:48:21)
- 12 (2007-09-27 (木) 18:52:16)
- 13 (2007-10-08 (月) 22:53:54)
- 14 (2008-02-26 (火) 22:17:41)
- 15 (2010-10-10 (日) 22:17:30)
- 16 (2010-10-11 (月) 18:18:12)
- 17 (2013-03-27 (水) 16:39:19)
- 18 (2014-12-16 (火) 14:21:15)
- 19 (2016-02-28 (日) 23:43:37)
- 20 (2016-09-22 (木) 21:22:32)
- 21 (2017-10-28 (土) 18:48:04)
- 22 (2018-12-24 (月) 18:35:56)
- 23 (2020-11-20 (金) 10:43:26)
- 24 (2022-12-16 (金) 13:56:00)
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:JTabbedPaneのタブを等幅にしてタイトルをクリップ #navi(../) *JTabbedPaneのタブを等幅にしてタイトルをクリップ [#r616bd2a] >編集者:[[Terai Atsuhiro>terai]]~ 作成日:2005-08-08~ 更新日:&lastmod; --- category: swing folder: ClippedTitleTab title: JTabbedPaneのタブを等幅にしてタイトルをクリップ tags: [JTabbedPane] author: aterai pubdate: 2005-08-08T01:49:21+09:00 description: JTabbedPaneのタブを等幅にし、長いタイトルはクリップして表示します。 image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTJXdZi5MI/AAAAAAAAAUQ/5nvfRoEEDEM/s800/ClippedTitleTab.png --- * 概要 [#summary] `JTabbedPane`のタブを等幅にし、長いタイトルはクリップして表示します。 #contents #download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTJXdZi5MI/AAAAAAAAAUQ/5nvfRoEEDEM/s800/ClippedTitleTab.png) **概要 [#fd7b15fa] JTabbedPaneのタブを等幅にし、長いタイトルはクリップして表示します。 #screenshot **サンプルコード [#z01f21c5] #code{{ * サンプルコード [#sourcecode] #code(link){{ final Insets tabInsets = UIManager.getInsets("TabbedPane.tabInsets"); tab1.setUI(new javax.swing.plaf.basic.BasicTabbedPaneUI() { @Override protected int calculateTabWidth(int tabPlacement, int tabIndex, FontMetrics metrics) { int width = tabPane.getWidth() - 4; //?4? switch(tabPlacement) { tab1.setUI(new BasicTabbedPaneUI() { @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) { @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; **解説 [#v0f32f18] 上記のサンプルでは、BasicTabbedPaneUI#calculateTabWidthメソッドをオーバーライドして、JTabbedPaneのタブ幅が、すべて等しくなるように設定しています。 * 解説 [#explanation] 上記のサンプルでは、`BasicTabbedPaneUI#calculateTabWidth(...)`メソッドをオーバーライドして、`JTabbedPane`のタブ幅がすべて等しくなるように設定しています。 タイトル文字列のほうが、このタブ幅より長い場合は、SwingUtilities.layoutCompoundLabelメソッドで文字列をクリップして表示します。 - タイトル文字列のほうがこのタブ幅より長い場合は`SwingUtilities.layoutCompoundLabel(...)`メソッドで文字列をクリップして表示 - タイトルがクリップされていてもツールチップで元の文字列を表示する - タブの位置を左右にした場合このサンプルでは全体の幅の`1/4`のタブ幅になるように設定 タイトルがクリップされていても、ツールチップで元の文字列を表示することができます。 #ref(https://lh3.googleusercontent.com/_9Z4BYR88imo/TQTJZ71XT7I/AAAAAAAAAUU/bO4iaEaR_xU/s800/ClippedTitleTab1.png) タブの位置を左右にした場合、このサンプルでは全体の幅の1/4のタブ幅になるようにしています。 #screenshot(,screenshot1.png) - `Java 1.6.0`以上の場合`JTabbedPane#setTabComponentAt(...)`メソッドでタブに`JLabel`を使用してそのデフォルトのクリップ機能が利用可能 -- [[JTabbedPaneのタイトルをクリップ>Swing/ClippedTabLabel]] JDK 6 なら、以下のようにJTabbedPane#setTabComponentAtメソッドを使って、JLabelのクリップ機能をそのまま利用する方法もあります。 #code{{ class MyJTabbedPane extends JTabbedPane { 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(); } } }} * 参考リンク [#reference] - [[JTabbedPaneのタイトルをクリップ>Swing/ClippedTabLabel]] - [[JTabbedPaneのTabTitleを左揃えに変更>Swing/TabTitleAlignment]] - [[JTabbedPaneのタブ文字列のあふれをフェードアウト効果に変更する>Swing/TextOverflowFadeTabbedPane]] //**参考リンク **コメント [#y18543e1] * コメント [#comment] #comment - `tabAreaInsets`を考慮するように修正しました。 -- &user(aterai); &new{2008-02-26 (火) 22:18:27}; #comment