Swing/SortingAnimations のバックアップソース(No.12)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/SortingAnimations へ行く。
- 1 (2007-07-09 (月) 14:59:22)
- 2 (2007-07-09 (月) 16:43:35)
- 3 (2007-07-10 (火) 12:13:28)
- 4 (2007-07-10 (火) 16:37:46)
- 5 (2009-08-12 (水) 16:32:41)
- 6 (2010-01-19 (火) 12:25:25)
- 7 (2011-05-08 (日) 23:29:56)
- 8 (2012-05-10 (木) 11:41:23)
- 9 (2013-02-02 (土) 21:36:51)
- 10 (2014-12-16 (火) 14:24:16)
- 11 (2016-03-18 (金) 15:04:27)
- 12 (2017-04-04 (火) 14:17:08)
- 13 (2017-07-20 (木) 14:12:50)
- 14 (2017-09-19 (火) 16:37:45)
- 15 (2019-03-26 (火) 19:05:08)
- 16 (2021-01-01 (金) 17:26:35)
- 17 (2023-05-01 (月) 04:32:17)
--- category: swing folder: SortingAnimations title: JComboBoxのモデルとしてenumを使用する tags: [JComboBox, Enum, Animation, SwingWorker] author: aterai pubdate: 2007-07-09T14:59:22+09:00 description: JComboBoxのモデルとしてenumを使用します。 image: https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTTcZXYeSI/AAAAAAAAAkY/_frjM9wSJsc/s800/SortingAnimations.png --- * 概要 [#summary] `JComboBox`のモデルとして`enum`を使用します。 #download(https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTTcZXYeSI/AAAAAAAAAkY/_frjM9wSJsc/s800/SortingAnimations.png) * サンプルコード [#sourcecode] #code(link){{ private static enum SortAlgorithms { Isort ("Insertion Sort"), Selsort ("Selection Sort"), Shellsort("Shell Sort"), Hsort ("Heap Sort"), Qsort ("Quicksort"), Qsort2 ("2-way Quicksort"); private final String description; private SortAlgorithms(String description) { this.description = description; } @Override public String toString() { return description; } } private final JComboBox algorithmsChoices = new JComboBox(SortAlgorithms.values()); //JDK 1.7.0 //private final JComboBox<Enum> algorithmsChoices = new JComboBox<Enum>(SortAlgorithms.values()); //private final JComboBox<? extends Enum> algorithmsChoices = new JComboBox<>(SortAlgorithms.values()); //private final JComboBox<SortAlgorithms> algorithmsChoices = new JComboBox<>(SortAlgorithms.values()); }} * 解説 [#explanation] `enum`型で`JComboBox`のモデルを作成しています。上記のコードでは、`Enum#toString()`メソッドをオーバーライドして、`JComboBox`の表示はユーザーに分かりやすい名前になるようにしています。 コード中で、どのアイテムが選択されているかなどを調べる場合などは、例えば以下のようにして使用します。 #code{{ switch ((SortAlgorithms) algorithmsChoices.getSelectedItem()) { case Isort: isort(number); break; case Selsort: ssort(number); break; case Shellsort: shellsort(number); break; case Hsort: heapsort(number); break; case Qsort: qsort(0, number - 1); break; case Qsort2: qsort2(0, number - 1); break; } }} ---- - ソートアニメーション自体は、%%[http://www.cs.bell-labs.com/cm/cs/pearls/sortanim.html Sorting Algorithm Animations from Programming Pearls]%%のアプレットから基本部分をコピーして`Swing`に移植 -- `SwingWorker`(`JDK 6`以上)を使用したキャンセル機能を追加 -- 全画面の書き換えを止めて、移動する点のみウェイトを入れて再描画するように変更 * 参考リンク [#reference] - %%[http://www.cs.bell-labs.com/cm/cs/pearls/sortanim.html Sorting Algorithm Animations from Programming Pearls]%% - [http://docs.oracle.com/javase/tutorial/uiswing/painting/ Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)] * コメント [#comment] #comment #comment