0
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?

KhoaVo
  • 376
  • 3
  • 18
  • You are assigning `true` to `isinserted ` in your if statement -> `if(isinserted = true)` – MadProgrammer Dec 03 '15 at 03:26
  • 1
    *"there was no changes in combo box when entering textfield"* - why should there be, all you did was change `isinserted` to `true`, you don't try loading any new data – MadProgrammer Dec 03 '15 at 03:27
  • oh I see, thank for pointing that out, I should have loaded data in to some temporary arraylist – KhoaVo Dec 03 '15 at 03:35
  • Well, assuming you want to update the combobox when the text field is changed, you should have updated the combobox within the `DocumentListener`, but this could be quite inefficent – MadProgrammer Dec 03 '15 at 03:37
  • I edited the code to load data, but I still don't see changes on jcombobox – KhoaVo Dec 03 '15 at 04:03
  • What's the point of updating `agegroupBox` when an item is selected from it, when you should be updating it from the text field changes? All you've done is load some data into an `ArrayList` – MadProgrammer Dec 03 '15 at 04:07
  • I want to search for rows from zipcode and age group, but there are so many zipcode so i made it a text field and use it to find preselected ageGroup, which then will both be used to find corresponding rows when user click submit – KhoaVo Dec 03 '15 at 04:09
  • Okay, but you still need to get those values into the combobox right? – MadProgrammer Dec 03 '15 at 04:12
  • yes, so basically what I should do is to move the whole ageGroupBox listener inside the changedUpdate method? – KhoaVo Dec 03 '15 at 04:13
  • That would update the combobox, but remember, this will happen every time you type a character – MadProgrammer Dec 03 '15 at 04:17
  • I see, how would I be able to change combobox only after I fully entered zipcode?, sorry this is my first time using listener in textfield, so I don't quite get it yet. – KhoaVo Dec 03 '15 at 04:20
  • Maybe something like [this](http://stackoverflow.com/questions/12189170/how-can-i-listen-for-keyboard-input-without-text-field-java/12189204#12189204) or something like [this](http://stackoverflow.com/questions/31666428/action-listener-for-a-jtextfield-to-change-value-in-another-textfield/31666660#31666660)? – MadProgrammer Dec 03 '15 at 04:23
  • I was wondering would it work if I check if the text is 5 character long before I load data into the box – KhoaVo Dec 03 '15 at 04:42
  • Yes, the first example I linked does something simular – MadProgrammer Dec 03 '15 at 04:43
  • Nvm, this is a stupid questions to ask, I didn't check the data carefully enough, there was only 3 possible types of AgeGroup, I dont know why I bothered to set up listener and such – KhoaVo Dec 03 '15 at 06:56

0 Answers0