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);
}
最佳答案
当我在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");
}
});