javajlist搜索中的奇怪行为
我已经用这种方法挣扎了好几个小时,它有一种奇怪的行为。 场景:两个jlist,menuList和productList,其中填充了来自mysql数据库的数据,第一个包含“类别”,第二个包含该类别的产品。它们都由对象向量填充。问题是,当我搜索一个产品时,它在所有情况下都能正常工作,除了在我保存新菜单(类别)之后。在本例中,菜单列表的索引成功地移动到我搜索的产品所在的正确类别,但productList仍然为空。 这是进行搜索的方法的代码javajlist搜索中的奇怪行为,java,mysql,swing,jlist,Java,Mysql,Swing,Jlist,我已经用这种方法挣扎了好几个小时,它有一种奇怪的行为。 场景:两个jlist,menuList和productList,其中填充了来自mysql数据库的数据,第一个包含“类别”,第二个包含该类别的产品。它们都由对象向量填充。问题是,当我搜索一个产品时,它在所有情况下都能正常工作,除了在我保存新菜单(类别)之后。在本例中,菜单列表的索引成功地移动到我搜索的产品所在的正确类别,但productList仍然为空。 这是进行搜索的方法的代码 private void bFindProductActio
private void bFindProductActionPerformed(java.awt.event.ActionEvent evt) {
Product product = new Product();
product.setProductName(searchedProduct.getText());
bNewProduct.setEnabled(false);
Vector<Product> productVoices = DBConnection.searchProduct(product);
if (!productVoices.isEmpty()) {
modelProductList.clear();
disableProductButtons();
menuName.setText("");
productName.setText("");
int idMenu = 0;
for (int i = 0; i < productVoices.size(); i++) {
Product current = (Product) productVoices.get(i);
modelProductList.addElement(current);
idMenu = current.getMenuId();
}
productList.setModel(modelProductList);
for (int i = 0; i < modelMenuList.getSize(); i++) {
Menu current = (Menu) modelMenuList.getElementAt(i);
if (idMenu == current.getMenuId()) {
int currentId = modelMenuList.indexOf(current);
menuList.setSelectedIndex(currentId);
menuList.requestFocus();
}
}
} else {
JOptionPane.showMessageDialog(null, "No product found", "", JOptionPane.WARNING_MESSAGE);
searchedProduct.setText("Search...");
}
}
谢谢。1。菜单是awt.Menu,如果是,则使用Swing JMenu,否则重命名此变量,2。我已经用这种方法挣扎了好几个小时,它有一种奇怪的行为。请确保我们也是,为了更好的帮助,请尽快发布一个SSCCE,简短的可运行,可编译,使用本地变量硬编码JList值谢谢,但同时我解决了这个问题。问题出在代码的另一部分。太复杂了,无法解释。
private void bSaveMenuActionPerformed(java.awt.event.ActionEvent evt) {
//If there's a selected menu updates
if (!menuList.isSelectionEmpty()) {
Menu selectedMenu = (Menu) menuList.getSelectedValue();
listIndex = menuList.getSelectedIndex();
selectedMenu.setMenuName(menuName.getText());
int result = DBConnection.updateMenu(selectedMenu);
if (result == 1) {
reloadMenuList(); //this re-fill the menuList
menuList.setSelectedIndex(listIndex);
menuList.setEnabled(true);
bFindProduct.setEnabled(true);
bNewMenu.setEnabled(true);
bNewProduct.setEnabled(false);
}
} else { //If there isn't a selected menu, creates a new menu
Menu newMenu = new Menu();
newMenu.setMenuName(menuName.getText());
int result = DBConnection.insertMenu(newMenu);
if (result == 1) {
reloadMenuList();
newMenuIsSaved = true;
menuList.setEnabled(true);
bFindProduct.setEnabled(true);
}
}
}