【问题标题】:How to link two Jlists如何链接两个 Jlist
【发布时间】:2014-04-14 14:09:17
【问题描述】:

我有两个向量的 Jlist,其中填充了从 mysql 数据库中提取的数据。我想要的是当用户从 Jlist1(我称之为 menuList)中选择一个项目(菜单)时,Jlist2(我称之为 productList)必须显示该菜单的产品和其他内容,例如在 THAT 中插入新产品的能力菜单或刚刚创建的新菜单。 我以一种我认为很弱的方式完成了这项任务,通过使用一些布尔变量来判断用户是在现有菜单中还是在新创建的菜单中插入产品。拜托,你能建议我一个更好的解决方案(如果存在的话)吗?这是代码最重要部分的摘录,这是在数据库中保存新产品的方法:

private void bAddProdActionPerformed(java.awt.event.ActionEvent evt) {                                         
    //If new menu is saved, get the new menu's Id

    if (newMenuIsSaved == true) {
        Product newProduct = new Product();
        newMenuId = DBConnection.getNewMenuId();
        newProduct.setMenuId(newMenuId);
        newProduct.setProductName(productName.getText());
        if (checkPPriceValidity(productPrice.getText(), newProduct)) {
            int result = DBConnection.insertProduct(newProduct);
            if (result == 1) {
                reloadProductList();

                disableProductButtons();
            }
        }

    } else {
        Product newProduct = new Product();
        //If a new menu wasn't saved, get the menuId from the selected one (from menuList):
        Menu selectedMenu = (Menu) menuList.getSelectedValue();
        newProduct.setMenuId(selectedMenu.getMenuId());
        newProduct.setProductName(productName.getText());
        if (checkPPriceValidity(productPrice.getText(), newProduct)) {
            int result = DBConnection.insertProduct(newProduct);
            if (result == 1) {
                reloadProductList();
                newMenuIsSaved = false;
                disableProductButtons();
                bNewProduct.setEnabled(true);

            }
        }
    }
}                                        

这里是reloadProductList()方法:

private void reloadProductList() {
    modelProductList.clear();

    if (newMenuIsSaved) {
        Vector<Product> productVoices = DBConnection.fillProductList(newMenuId);
        for (int i = 0; i < productVoices.size(); i++) {
            modelProductList.addElement((Product) productVoices.get(i));
        }
    } else {
        Vector<Product> productVoices = DBConnection.fillProductList(selectedMenuId);
        for (int i = 0; i < productVoices.size(); i++) {
            modelProductList.addElement((Product) productVoices.get(i));
        }

    }
}

非常感谢。

【问题讨论】:

    标签: java mysql swing jlist


    【解决方案1】:

    一种方法是将ListSelectionListener 添加到menuList 并让处理程序设置其他 列表的模型以显示所选行的详细信息。

    【讨论】:

    • 谢谢你,但你能不能给我更多的细节或任何例子?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-24
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-29
    相关资源
    最近更新 更多