JComboBox没有显示更新的模型,如何刷新组合框

时间:2014-03-18 09:57:05

标签: java jcombobox

JCombo框没有显示arraylist的更新值,其中当我打印数组时,它显示它已更新为新增值我使用DefaultComboBoxModel添加数组

请帮助我如何将更改的内容触发到UI

JComboBox comboBox_CHR = new JComboBox();
File CHRXml = new File("CHR.xml");
ArrayList<String> chrNo = xml.readChrNum(CHRXml);
DefaultComboBoxModel model=new DefaultComboBoxModel(chrNo.toArray());

public MainPage() 
{
  ArrayList<String> chrNo = xml.readChrNum(CHRXml);
  chrNo = xml.readChrNum(CHRXml);
  model=new DefaultComboBoxModel(chrNo.toArray());  
  System.out.println(chrNo);
  comboBox_CHR.setModel(model);
  comboBox_CHR.setModel(model);
  comboBox_CHR.setBackground(new Color(176, 196, 222));
  comboBox_CHR.setBounds(105, 50, 348, 30);
  panel_Chr.add(comboBox_CHR);
}

1 个答案:

答案 0 :(得分:1)

当我在EDT上进行更新时,它终于工作了

addComponentListener(new ComponentAdapter() 
{ 
@Override 
public void componentShown(ComponentEvent arg0)
 { 
if(CHRXml.exists())
{ 
ArrayList<String> chrNo = xml.readChrNum(CHRXml);
 chrNo = xml.readChrNum(CHRXml); 
comboBox_CHR.setModel(new DefaultComboBoxModel(chrNo.toArray()));
} 
else 
JOptionPane.showMessageDialog(null,"CHR database file doesnot exists");
 } 
});