多个 jcombobox 的问题

发布于 2024-11-10 04:30:54 字数 3197 浏览 2 评论 0原文

我一直在处理一个需要我使用多个 jcombobox 的项目。我确实尝试链接​​三个 jcombobox,但未能显示所有必要的下拉列表。

在我的一个组合框中,我有银行列表(Bank1、Bank2),另一个是已选择的特定银行中所有分行的列表(Bank1(branch1-1、branch1-2)、Bank2(branch2-1) 、branch2-2)) 和最后一个是已选择的所有特定分支的账户号。每个分行都有多个账户。

我使用 2 个组合框没有问题,所有分支都显示为已选择的特定银行,但是,当我添加第三个组合框(即帐户 #)时,只从我的数据库查询一个分支。前任。如果我选择 Bank1,则只有“branch1”会出现在列表中,如果选择 Bank2,则只有branch2-1 也会出现在列表中,但该特定分行的账户号会出现在下拉列表中。

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;
}

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌伤浅笑 2024-11-17 04:30:54

问题解决了。我对所有结果集使用单一变量。啧!感谢您回复我的帖子。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文