Swing/SortTabs のバックアップソース(No.6)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/SortTabs へ行く。
- 1 (2006-04-24 (月) 15:47:22)
- 2 (2006-11-10 (金) 00:12:58)
- 3 (2007-08-28 (火) 12:18:25)
- 4 (2012-10-27 (土) 05:42:27)
- 5 (2013-03-11 (月) 18:27:55)
- 6 (2014-11-20 (木) 01:52:41)
- 7 (2015-02-26 (木) 13:59:40)
- 8 (2016-12-20 (火) 13:59:38)
- 9 (2017-11-09 (木) 17:56:36)
- 10 (2019-06-07 (金) 15:32:57)
- 11 (2021-02-23 (火) 18:07:14)
- 12 (2023-04-30 (日) 12:17:36)
--- title: JTabbedPaneのタブをソート tags: [JTabbedPane] author: aterai pubdate: 2006-04-24T15:47:22+09:00 description: JTabbedPaneのタブをソートします。 --- * 概要 [#bb32089f] `JTabbedPane`のタブをソートします。 #download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTTe98QmaI/AAAAAAAAAkc/w7tzozy5FqM/s800/SortTabs.png) * サンプルコード [#ue218b7d] #code(link){{ class SortAction extends AbstractAction { public SortAction(String label, Icon icon) { super(label,icon); } @Override public void actionPerformed(ActionEvent evt) { setSortedTab(tab, makeSortedVector(tab)); } private Vector makeSortedVector(JTabbedPane t) { Vector l = new Vector(); for(int i=0;i<t.getTabCount();i++) { l.addElement(new ComparableTab(t.getTitleAt(i), t.getComponentAt(i))); } Collections.sort(l); return l; } private void setSortedTab(final JTabbedPane t, final Vector l) { t.setVisible(false); t.removeAll(); for(int i=0;i<l.size();i++) { ComparableTab c = (ComparableTab)l.get(i); t.addTab(c.title, c.comp); } t.setVisible(true); } class ComparableTab implements Comparable{ final public String title; final public Component comp; public ComparableTab(String title, Component comp) { this.title = title; this.comp = comp; } @Override public int compareTo(Object o) { return this.title.compareTo(((ComparableTab)o).title); } } } }} * 解説 [#g820882f] 上記のサンプルでは、ソートしたリストを作成したあと、一旦タブをすべて削除し、リストから`JTabbedPane`にタブを戻しています。 タブを追加、削除、ダブルクリックで名前変更して確認してみてください。 //* 参考リンク * コメント [#v048c1a4] #comment #comment