Swing/SortTabs のバックアップの現在との差分(No.1)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - 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のタブをソート #navi(../) *JTabbedPaneのタブをソート [#bb32089f] >編集者:[[Terai Atsuhiro>terai]]~ 作成日:2006-04-24~ 更新日:&lastmod; --- 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`のタブタイトルでその並び順をソートします。 #contents #download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTTe98QmaI/AAAAAAAAAkc/w7tzozy5FqM/s800/SortTabs.png) **概要 [#ib52ab9f] JTabbedPaneのタブをソートします。 * サンプルコード [#sourcecode] #code(link){{ class SortAction extends AbstractAction { @Override public void actionPerformed(ActionEvent e) { JTabbedPane t = (JTabbedPane) getInvoker(); List<ComparableTab> list = IntStream.range(0, t.getTabCount()) .mapToObj(i -> new ComparableTab(t.getTitleAt(i), t.getComponentAt(i))) .sorted(Comparator.comparing(ComparableTab::getTitle)) .collect(Collectors.toList()); t.removeAll(); list.forEach(c -> t.addTab(c.getTitle(), c.getComponent())); } } #screenshot class ComparableTab { private final String title; private final Component comp; **サンプルコード [#ue218b7d] class SortAction extends AbstractAction { public SortAction(String label, Icon icon) { super(label,icon); } public void actionPerformed(ActionEvent evt) { final Vector list = makeSortedVector(tab); javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { setSortedTab(tab, list); } }); } 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; } public int compareTo(Object o) { return this.title.compareTo(((ComparableTab)o).title); } } } protected ComparableTab(String title, Component comp) { this.title = title; this.comp = comp; } -&jnlp; -&jar; -&zip; public String getTitle() { return title; } **解説 [#g820882f] 上記のサンプルでは、ソートしたリストを作成したあと、一旦タブをすべて削除し、リストからJTabbedPaneにタブを戻しています。 public Component getComponent() { return comp; } } }} //**参考リンク **コメント [#v048c1a4] * 解説 [#explanation] 上記のサンプルでは、タブを追加、削除、ダブルクリックで名前変更してタブタイトルでのソートがテスト可能です。 - タブのソートは以下の手順で実行 + ソートしたタブのリストを作成 + `JTabbedPane`から一旦タブをすべて削除 + ソート済みのリストから`JTabbedPane`にタブを戻す * 参考リンク [#reference] - [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/JTabbedPane.html#removeAll-- JTabbedPane#removeAll() (Java Platform SE 8)] * コメント [#comment] #comment #comment