public class SelectFrame extends JFrame {
private JPanel contentPane;
private JTextField zipText;
JComboBox<String> agegroupBox;
HealthData data = new HealthData();
private boolean isinserted;
ArrayList<String> list;
/**
* Create the frame.
*/
public SelectFrame() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 450, 150);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
agegroupBox = new JComboBox();
zipText = new JTextField();
zipText.setColumns(10);
zipText.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
isinserted = true;
if(validation())
{
int zipcode = Integer.parseInt(zipText.getText());
list = new ArrayList<String>(data.selectAgeGroup(zipcode));
}
}
@Override
public void removeUpdate(DocumentEvent e) {
// TODO Auto-generated method stub
}
@Override
public void changedUpdate(DocumentEvent e) {
agegroupBox.addActionListener(new ActionListener () {
public void actionPerformed(ActionEvent e) {
if(isinserted = true)
{
for(int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).toString());
agegroupBox.addItem(list.get(i));
}
}
}
});
}
public boolean validation()
{
try {
int zipcode = Integer.parseInt(zipText.getText());
}catch (NumberFormatException a) {
JOptionPane.showMessageDialog(null, "Zipcode entered is invalid", "Error", JOptionPane.ERROR_MESSAGE);
new SelectFrame();
}
return true;
}
});
agegroupBox.addActionListener(new ActionListener () {
public void actionPerformed(ActionEvent e) {
if(isinserted = true)
{
for(int i = 0; i < list.size(); i++)
agegroupBox.addItem(list.get(i));
}
}
});
我试图在文本字段中插入zipcode时将数据从数据库加载到comboBox中,我正在使用2个侦听器进行组合框和文本字段,但是当我运行此代码时,组合框中输入文本字段时没有任何变化,如果将zipcode插入文本字段,如何更新组合框?