1

I've been working with a project that requires me to use mutiple jcombobox. I did try to chain three jcombobox but failed to show all necessary drop-down lists.

In one of my combobox I have lists of Banks (Bank1, Bank2), the other one is the list of all branches in a specific Bank that has been selected (Bank1(branch1-1, branch1-2), Bank2(branch2-1, branch2-2)) and the last one are the account # for all specific branches that has been selected. Each branches has a multiple accounts.

I have no problem working with 2 comboboxes, all branches are shown for a specific Bank that has been selected, but, when I added the third combobox which is the account #, only one branch is being queried from my db. ex. if I select Bank1 only "branch1" will be on the list, and if Bank2 only branch2-1 will be on the list also, but account # for that specific branches are on the drop-down lists.

private void populateSavingsAccountComboBox() {
    accountNo.removeAllItems();
    bankBranch.removeAllItems();
    selectBank();
    bankName.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            String bank = bankName.getSelectedItem() == null ?
                "" : bankName.getSelectedItem().toString();
            selectBranch(bank);
        }
    });

    bankBranch.addItemListener(new ItemListener() {

        public void itemStateChanged(ItemEvent e) {
            Object source = e.getSource();
            JComboBox target = (JComboBox) e.getSource();
            String branch = target.getSelectedItem() == null ?
                "" : target.getSelectedItem().toString();
            if (e.getStateChange() == ItemEvent.SELECTED) {
                selectAccountNo(bankName.getSelectedItem().toString(), branch);
            }
        }
    });
}

private void selectBank() {
    List bankList = new ArrayList();
    try {
        stmt = conn.createStatement();
        rs = stmt.executeQuery(" SELECT bankName FROM bank_tbl ");
        bankName.removeAllItems();
        while (rs.next()) {
            String bank = rs.getString("bankName");
            bankList.add(bank);
            Object bankElement = bankList.get(bankList.size() - 1);
            bankName.addItem(bankElement);
        }
    } catch (SQLException ex) {
        Logger.getLogger(addSavings.class.getName()).log(Level.SEVERE, null, ex);
    }
}

private String selectBranch(String bank) {
    try {
        List branchList = new ArrayList();
        rs = stmt.executeQuery(" SELECT branch FROM bank_branch_tbl WHERE "
            + " bankName = '" + bank + "' ");
        bankBranch.removeAllItems();
        while (rs.next()) {
            branchList.add(rs.getString("branch"));
            Object branchElement = branchList.get(branchList.size() - 1);
            bankBranch.addItem(branchElement);
        }
    } catch (SQLException ex) {
        Logger.getLogger(addContact.class.getName()).log(Level.SEVERE, null, ex);
    }
    return bank;
}

private String selectAccountNo(String bank, String branch) {
    List accountNoList = new ArrayList();
    try {
        rs = stmt.executeQuery(" SELECT accountNo FROM account_no_tbl WHERE "
            + " bankName = '" + bank + "' AND "
            + " branch = '" + branch + "' ");
        accountNo.removeAllItems();
        while (rs.next()) {
            accountNoList.add(rs.getString("accountNo"));
            Object accountNoElement = accountNoList.get(accountNoList.size() - 1);
            accountNo.addItem(accountNoElement);
        }
    } catch (SQLException ex) {
        Logger.getLogger(addSavings.class.getName()).log(Level.SEVERE, null, ex);
    }
    return branch;
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Jet Beray
  • 21
  • 2
  • please indent your code, it's hard to read it like that. – MByD May 25 '11 at 08:17
  • 1
    @Jet Beray welcome on this forum – mKorbel May 25 '11 at 08:20
  • 10s I have indented the code, Thanks to Mr. NetBeans. The edit is waiting for approval. – Boro May 25 '11 at 08:54
  • 1
    @Jet Beray can you please provide us an SSCCE which we could play with. Obviously excluding all database related stuff. I assume that the data in the tables is correct, as at the quick look the sql's seem fine. – Boro May 25 '11 at 09:26
  • 1
    For your [sscce](http://sscce.org), you could start with this [example](http://stackoverflow.com/questions/3191837/dynamic-jcomboboxes/3191882#3191882). – trashgod May 25 '11 at 09:55
  • @Boro: Good edit, but you changed the poster's [brace style](http://junit.sourceforge.net/doc/faq/faq.htm#organize_1)! :-) – trashgod May 25 '11 at 12:15
  • @trashgod I think that "nothing there" because I send only half size answer to OP (two week old thread from another Java Forum), but there isn't about crossposting oldiest that 1W, and that's my bad that I leave ... http://www.daniweb.com/software-development/java/threads/363152 – mKorbel May 25 '11 at 12:32
  • @trashgod I most certainly did. Didn't consider it a problem. The current one is slightly more concise but I don't like it personally :) – Boro May 25 '11 at 12:52
  • hi guys, my apology for not editing it well, im a newbie in this forum. btw mKorbel, you where also the one posting solutions in daniweb right? I already run out of ideas, im not sure if the problem is in event handler or in my query, since I did try to debug it, it shows only one data for branches each time I include account nos. it seems that the query from branches halt at the first data when the account no is loaded but failed to continue the remaining branches after the account no. data are on the list. – Jet Beray May 26 '11 at 01:59
  • problem solved. I was using one variable for all ResultSet. tsk! thank you for replying to my post. – Jet Beray May 26 '11 at 09:03
  • @Jet you should post your solution as an answer to the question and then accept it, so this falls off the list of Unanswered questions. – I82Much Jun 02 '11 at 18:44

1 Answers1

1

problem solved. I was using one-single variable for all ResultSet. tsk! thank you for replying to my post.

Jet Beray
  • 21
  • 2