の項目を無効にする方法はありませんJComboBox
。ここの場所から削除する方法は次のとおりです。-
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class Combobox extends JFrame{
Combobox(){
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] list={"car","bus","bike"};
final JComboBox c1=new JComboBox(list);
final JComboBox c2=new JComboBox(list);
Container c=this.getContentPane();
c.setLayout(new FlowLayout());
c.add(c1);
c.add(c2);
c1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
int index=c1.getSelectedIndex();
c2.removeItemAt(index);
}
});
this.pack();
}
public static void main(String[] args) {
new Combobox();
}
}
final JComboBox c1=new JComboBox(list);
JComboBox
のアイテムを作成しlist
ます。c1は、クリックイベントに使用さfinal
れる内部クラス内で呼び出されるために使用されます。で選択したアイテムのを取得します。c2の場所にあるアイテムを削除します。と両方に類似したアイテムが含まれているため、アイテムの位置は同じです。ある時点でアイテムをc2に再挿入する場合は、削除するアイテムのインデックスの場所と、を使用して削除するアイテムの名前を保存します。ActionListener
index=c1.getSelectedIndex();
index location
c1
c2.removeItemAt(index);
index
c1
c2
index
index=c1.getSelectedIndex();
item=c2.getItemAtIndex(index);
c2.removeItemAt(index);
次に、を使用してアイテムを復元します
c2.insertItemAt(item,index);
注-外部で使用する場合は、外部で宣言する必要が index
あります。item
ActionListener