• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JComboBoxのモデルとしてenumを使用する
#navi(../)
RIGHT:Posted by [[terai]] at 2007-07-09
*JComboBoxのモデルとしてenumを使用する [#y688293a]
JComboBoxのモデルとしてenumを使用します。
---
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`を使用します。

-&jnlp;
-&jar;
-&zip;
#download(https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTTcZXYeSI/AAAAAAAAAkY/_frjM9wSJsc/s800/SortingAnimations.png)

#screenshot

**サンプルコード [#i89c933e]
#code{{
* サンプルコード [#sourcecode]
#code(link){{
private static enum SortAlgorithms {
  Isort    ("Insertion Sort"),
  Selsort  ("Selection Sort"),
  Isort("Insertion Sort"),
  Selsort("Selection Sort"),
  Shellsort("Shell Sort"),
  Hsort    ("Heap Sort"),
  Qsort    ("Quicksort"),
  Qsort2   ("2-way Quicksort");
  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());
JComboBox<SortAlgorithms> algorithmsChoices =
    new JComboBox<>(SortAlgorithms.values());
}}

**解説 [#q914d563]
enum型でJComboBoxのモデルを作成しています。上記のコードでは、Enum#toString()メソッドをオーバーライドして、JComboBoxの表示はユーザーに分かりやすい名前になるようにしています。
* 解説 [#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;
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;
}
}}

ソートアニメーション自体は、[[Sorting Algorithm Animations from Programming Pearls>http://www.cs.bell-labs.com/cm/cs/pearls/sortanim.html]]から基本部分はそのままコピーして、Swingに移植しています。
-SwingWorkerでキャンセル可能に
--このため、実行するには、JDK 6 以上が必要
-ゴミが残らないよう全部の点を書き換えている
--点が増えるとオリジナルに比べてかなり遅い((速すぎず、見やすいような気もするので…))
-アニメーション中は、paintComponentsを使わず、自前のダブルバッファリングを使用
--[[アクティブレンダリング - Javaでゲーム作りますが何か?>http://javagame.main.jp/index.php?%A5%A2%A5%AF%A5%C6%A5%A3%A5%D6%A5%EC%A5%F3%A5%C0%A5%EA%A5%F3%A5%B0]]
----
- ソートアニメーション自体は%%[http://www.cs.bell-labs.com/cm/cs/pearls/sortanim.html Sorting Algorithm Animations from Programming Pearls]%%のアプレットから基本部分をコピーして`Swing`に移植
-- `SwingWorker`(`JDK 6`以上)を使用したキャンセル機能を追加
-- 全画面の書き換えを止めて移動する円図形の領域のみウェイトを入れて再描画するように変更

**参考リンク [#z405b484]
-[[Sorting Algorithm Animations from Programming Pearls>http://www.cs.bell-labs.com/cm/cs/pearls/sortanim.html]]
-[[アクティブレンダリング - Javaでゲーム作りますが何か?>http://javagame.main.jp/index.php?%A5%A2%A5%AF%A5%C6%A5%A3%A5%D6%A5%EC%A5%F3%A5%C0%A5%EA%A5%F3%A5%B0]]
* 参考リンク [#reference]
- %%[http://www.cs.bell-labs.com/cm/cs/pearls/sortanim.html Sorting Algorithm Animations from Programming Pearls]%%
- [https://docs.oracle.com/javase/tutorial/uiswing/painting/ Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)]
- [[Enum定数を選択するJRadioButtonのButtonGroupやJComboBoxを作成する>Swing/EnumInputSelect]]

**コメント [#bedcfd09]
* コメント [#comment]
#comment
#comment