0
私はJComboBoxでアイテムを更新するのに苦労しています。ファイルからアイテムを読み込むと、コンボボックスが正しく表示されますが、コンボボックスアイテムからアイテムを追加または削除しようとすると、コンボボックスが自動的に更新されず、代わりに同じアイテムが残ります。ここに私のコード は、私はこれはアイテムJComboBoxを自動的に更新する方法
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
for (int i = 0; i < diary.getUnitCollection().size(); i++){
if (jComboBox8.getSelectedItem().equals(diary.getUnitCollection().get(i).getUnitName())){
diary.getUnitCollection().remove(diary.getUnitCollection().get(i));
jTextArea3.setText("The Unit " + jComboBox8.getSelectedItem()+ " has been removed successfully");
}
}
}
編集を削除するためのボタンであるコンボボックスの項目に
ObjectInputStream input;
try {
// TODO add your handling code here:
JFileChooser openFileChooser = new JFileChooser();
openFileChooser.setCurrentDirectory(new File("."));
if (openFileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
input = new ObjectInputStream(new FileInputStream(openFileChooser.getSelectedFile()));
diary = (Diary)input.readObject();
jTextArea3.setText(diary.getUnitCollection().toString());
input.close();
//Load Unit Item
for (Unit u: diary.getUnitCollection()){
jComboBox8.addItem(u.getUnitName());
jComboBox1.addItem(u.getUnitName());
}
}
} catch (ClassNotFoundException ex) {
Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
}
をロードする場所ですです:ちょうどあなたが追加する必要があり、スタイル(コード・ブロック)
モデルをJComboBoxに追加するには、combobox.setModel(theModel)を使用します。 – Martin
アイテムが固定されていないため、アイテムを固定していないため、アイテムをロードする方法 – babygau
@TruongThanhDung:Martinが上に挙げたAPIを見て、DefaultComboBoxModelオブジェクトで使用できるメソッドを使用します。マーティンに1+ –