Java SwingのJComboBoxを用いてHtmlにおけるSelectのkey-value機能を実現


1、カスタムリスト項目クラスItem
public class Item {
	private int key;
	
	private String value;
	
	public Item(int key, String value){
		this.key = key;
		this.value = value;
	}


	public void setKey(int key) {
		this.key = key;
	}


	public int getKey() {
		return key;
	}


	public void setValue(String value) {
		this.value = value;
	}


	public String getValue() {
		return value;
	}
	
	public String toString(){
		return value;
	}
}

2、JComboBoxにデータを挿入する
Item item = new Item(key, value);
cb.addItem(item);

3、現在の選択項目を取得する
Item item = (Item)cb.getSelectedItem();
item.getKey()とitem.getValue()を呼び出してkeyとvalue値を取得