---
category: swing
folder: LoopComboBox
title: JComboBoxのItem選択をループ
tags: [JComboBox, ActionMap, InputMap]
author: aterai
pubdate: 2005-10-24T09:40:47+09:00
description: JComboBoxのItemの選択が、上下のカーソルキーでループするように設定します。
image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTPicRK7pI/AAAAAAAAAeI/ApRsPHlRWe0/s800/LoopComboBox.png
---
* 概要 [#summary]
`JComboBox`の`Item`の選択が、上下のカーソルキーでループするように設定します。

#download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTPicRK7pI/AAAAAAAAAeI/ApRsPHlRWe0/s800/LoopComboBox.png)

* サンプルコード [#sourcecode]
#code(link){{
Action up = new AbstractAction() {
  @Override public void actionPerformed(ActionEvent e) {
    int index = combo.getSelectedIndex();
    combo.setSelectedIndex((index == 0) ? combo.getItemCount() - 1 : index - 1);
  }
};
Action down = new AbstractAction() {
  @Override public void actionPerformed(ActionEvent e) {
    int index = combo.getSelectedIndex();
    combo.setSelectedIndex((index == combo.getItemCount() - 1) ? 0 : index + 1);
  }
};
ActionMap amc = combo.getActionMap();
amc.put("myUp",   up);
amc.put("myDown", down);
InputMap imc = combo.getInputMap();
imc.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0),   "myUp");
imc.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "myDown");
}}

* 解説 [#explanation]
上記のサンプルでは、下のコンボボックスの`ActionMap`と`InputMap`を使って、上下キーに対応する新しいアクションを設定しています。

//* 参考リンク [#reference]
* コメント [#comment]
#comment
#comment