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));
}
}
});
Im trying to load data from a database into comboBox when zipcode is inserted in the the text field, I'm using 2 listeners for combobox and textfield, but when I ran this code there was no changes in combo box when entering textfield, How can I make combobox update when a zipcode is inserted into the textfield?