Swing/SortTabs のバックアップ差分(No.10)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - 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)
- 追加された行はこの色です。
- 削除された行はこの色です。
--- category: swing folder: SortTabs title: JTabbedPaneのタブをソート tags: [JTabbedPane] author: aterai pubdate: 2006-04-24T15:47:22+09:00 description: JTabbedPaneのタブタイトルでその並び順をソートします。 image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTTe98QmaI/AAAAAAAAAkc/w7tzozy5FqM/s800/SortTabs.png --- * 概要 [#summary] `JTabbedPane`のタブタイトルでその並び順をソートします。 #download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTTe98QmaI/AAAAAAAAAkc/w7tzozy5FqM/s800/SortTabs.png) * サンプルコード [#sourcecode] #code(link){{ class SortAction extends AbstractAction { public SortAction(String label, Icon icon) { super(label, icon); } @Override public void actionPerformed(ActionEvent e) { setSortedTab(tab, makeSortedVector(tab)); JTabbedPane tabbedPane = (JTabbedPane) getInvoker(); List<ComparableTab> list = IntStream.range(0, tabbedPane.getTabCount()) .mapToObj(i -> new ComparableTab(tabbedPane.getTitleAt(i), tabbedPane.getComponentAt(i))) .sorted(Comparator.comparing(ComparableTab::getTitle)).collect(Collectors.toList()); tabbedPane.removeAll(); list.forEach(c -> tabbedPane.addTab(c.getTitle(), c.getComponent())); } 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; } class ComparableTab { private final String title; private final Component comp; protected ComparableTab(String title, Component comp) { this.title = title; this.comp = comp; } 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); public String getTitle() { return title; } 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); } public Component getComponent() { return comp; } } }} * 解説 [#explanation] 上記のサンプルでは、以下の手順でタブのソートを行っています。タブを追加、削除、ダブルクリックで名前変更などが可能なので、確認してみてください。 - ソートしたタブのリストを作成 - `JTabbedPane`から一旦タブをすべて削除 - ソート済みのリストから`JTabbedPane`にタブを戻す //* 参考リンク [#reference] * 参考リンク [#reference] - [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/JTabbedPane.html#removeAll-- JTabbedPane#removeAll() (Java Platform SE 8)] * コメント [#comment] #comment #comment