Swing/ClippedTabLabel のバックアップ差分(No.4)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/ClippedTabLabel へ行く。
- 1 (2007-10-08 (月) 22:56:05)
- 2 (2007-11-30 (金) 14:15:47)
- 3 (2008-02-26 (火) 22:11:28)
- 4 (2008-03-22 (土) 12:44:57)
- 5 (2008-03-24 (月) 17:27:13)
- 6 (2008-10-30 (木) 22:00:40)
- 7 (2008-12-16 (火) 21:44:18)
- 8 (2009-11-12 (木) 21:38:34)
- 9 (2010-10-08 (金) 15:45:04)
- 10 (2010-10-08 (金) 22:09:21)
- 11 (2010-10-11 (月) 18:18:27)
- 12 (2012-01-05 (木) 20:23:28)
- 13 (2012-01-05 (木) 21:35:02)
- 14 (2012-06-24 (日) 05:18:37)
- 15 (2013-01-30 (水) 23:34:00)
- 16 (2014-08-27 (水) 21:04:24)
- 17 (2014-10-31 (金) 01:41:41)
- 18 (2014-11-01 (土) 00:46:09)
- 19 (2014-11-28 (金) 16:02:59)
- 20 (2015-02-26 (木) 13:58:31)
- 21 (2015-06-22 (月) 17:58:40)
- 22 (2017-03-17 (金) 20:08:15)
- 23 (2017-03-31 (金) 16:11:45)
- 24 (2017-11-02 (木) 15:34:40)
- 25 (2018-02-24 (土) 19:51:30)
- 26 (2018-03-06 (火) 17:07:49)
- 27 (2020-03-13 (金) 22:13:50)
- 28 (2021-09-19 (日) 10:50:33)
- 29 (2022-08-20 (土) 22:15:25)
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:JTabbedPaneのタイトルをクリップ #navi(../) *JTabbedPaneのタイトルをクリップ [#paeaad58] >編集者:[[Terai Atsuhiro>terai]]~ 作成日:2007-10-08~ 更新日:&lastmod; #contents **概要 [#zcdc1d6d] JDK 6 で導入されたタブにコンポーネントを追加する機能を使って、長いタイトルのタブは文字列をクリップして表示します。 #screenshot **サンプルコード [#xad70fa1] #code{{ class 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(); } }); addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent 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(); } private void initTabWidth() { int paneWidth = getWidth() - 4; //?4? int tabCount = getTabCount(); int tabPlacement = getTabPlacement(); int tabWidth = tabInsets.left + tabInsets.right + 3; switch(tabPlacement) { 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)(paneWidth/4) - tabWidth; tabWidth = (int)(areaWidth/4) - tabWidth; break; case BOTTOM: case TOP: default: tabWidth = (int)(paneWidth/tabCount) - tabWidth; tabWidth = (int)(areaWidth/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)); } if(l==null) break; l.setPreferredSize(new Dimension(tabWidth, l.getPreferredSize().height)); } revalidate(); } } }} -&jnlp; -&jar; -&zip; **解説 [#xe7c3559] 下のJTabbedPaneでは、タブにJTabbedPane#setTabComponentAtメソッドを使ってJLabelを追加し、そのクリップ機能を利用して長いタイトル文字列をクリップしています。 [[JTabbedPaneのタブを等幅にしてタイトルをクリップ>Swing/ClippedTitleTab]]と、ほぼ同等((文字列の長さがばらばらでも、左右にタブをおいた場合は全体の1/4の幅に、上下にタブをおいた場合はすべてのタブ幅が均等になる))ですが、TabbedPaneUI#paintTextで文字列の描画をオーバーライドする必要も無く、ソースも短くて実装が簡単です。 **参考リンク [#r6054f8d] -[[JTabbedPaneのタブを等幅にしてタイトルをクリップ>Swing/ClippedTitleTab]] **コメント [#p47877ec] - 以下の二点を変更しました: tabAreaInsetsを考慮するように修正、TOP-LEFTの切り替えの追加 -- [[terai]] &new{2008-02-26 (火) 22:15:27}; #comment