JComboBox ユーザーが選択したコンボボックスの値が、望ましくない場合、あと戻りさせるためのコードです。
2007/12/10
java
swing
Javaです。
ユーザーが選択したコンボボックスの値が、望ましくない場合、あと戻りさせるためのコードです。
JComboBoxにItemListenerをaddして、
ItemEventのDESELECTEDがきた場合、oldの値として以前のデータをおぼえるようにします。
SELECTEDがきた場合に、その変更が適切なものか判断させて、もとにもどすという処理で実現してみました。
追記:あとから気づいて、SwingUtilities.invokeLaterでくるみました。
他にいい方法があればいいのですが。
package comb;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JPanel;
public class TestComb extends JPanel {
private static final long serialVersionUID = 1L;
private JComboBox jComboBox = null;
public TestComb() {
super();
initialize();
}
private void initialize() {
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.fill = GridBagConstraints.VERTICAL;
gridBagConstraints.gridy = 0;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.gridx = 0;
this.setSize(300, 200);
this.setLayout(new GridBagLayout());
this.add(getJComboBox(), gridBagConstraints);
}
private JComboBox getJComboBox() {
if (jComboBox == null) {
jComboBox = new JComboBox();
jComboBox.addItem("aaaaaa");
jComboBox.addItem("bbbbbb");
jComboBox.addItem("cccccc");
jComboBox.addItem("dddddd");
jComboBox.addItemListener(new ItemListener(){
Object old = null;
public void itemStateChanged(ItemEvent e) {
System.out.println(e);
if(e.getStateChange() == ItemEvent.DESELECTED){
this.old = e.getItem();
return;
}
if(eq(e.getItem(), "bbbbbb")){
System.out.println("change old value!!");
SwingUtilities.invokeLater(new Runnable(){
public void run() {
comboBox.setSelectedItem(old);
}});
return;
}
}});
}
return jComboBox;
}
protected static boolean eq(Object object, Object object2) {
return object == null ? object2 == null : object.equals(object2);
}
}
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JPanel;
public class TestComb extends JPanel {
private static final long serialVersionUID = 1L;
private JComboBox jComboBox = null;
public TestComb() {
super();
initialize();
}
private void initialize() {
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.fill = GridBagConstraints.VERTICAL;
gridBagConstraints.gridy = 0;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.gridx = 0;
this.setSize(300, 200);
this.setLayout(new GridBagLayout());
this.add(getJComboBox(), gridBagConstraints);
}
private JComboBox getJComboBox() {
if (jComboBox == null) {
jComboBox = new JComboBox();
jComboBox.addItem("aaaaaa");
jComboBox.addItem("bbbbbb");
jComboBox.addItem("cccccc");
jComboBox.addItem("dddddd");
jComboBox.addItemListener(new ItemListener(){
Object old = null;
public void itemStateChanged(ItemEvent e) {
System.out.println(e);
if(e.getStateChange() == ItemEvent.DESELECTED){
this.old = e.getItem();
return;
}
if(eq(e.getItem(), "bbbbbb")){
System.out.println("change old value!!");
SwingUtilities.invokeLater(new Runnable(){
public void run() {
comboBox.setSelectedItem(old);
}});
return;
}
}});
}
return jComboBox;
}
protected static boolean eq(Object object, Object object2) {
return object == null ? object2 == null : object.equals(object2);
}
}
: