[英]Searching trough jList with key events
I have a problem with my Java code.我的 Java 代码有问题。 I am trying to search trough a
JList
, with key events, but it seems like it can't find anything.我正在尝试通过
JList
搜索关键事件,但似乎找不到任何东西。
With this event, I am adding values to my JList
:通过这个事件,我向我的
JList
添加值:
DefaultListModel<String> model = new DefaultListModel<>();
private void productButtonActionPerformed(java.awt.event.ActionEvent evt) {
for (Shop i : shopSettings.Products) {
model.addElement(i.getProductName()+i.getPrice()+i.getProductCategory()+i.getNumber());
}
jList1.setModel(model);
}
Here I am trying to search trough the JList
, with this action, but anytime I write something, it just overwrites the list, and displays nothing.在这里,我试图通过此操作搜索
JList
,但每当我写一些东西时,它只会覆盖列表,并且什么也不显示。
private void jTextField1KeyReleased(java.awt.event.KeyEvent evt) {
DefaultListModel filteredProducts = new DefaultListModel();
for (Shop i: shopSettings.Products) {
String productName=i.getProductName()+i.getPrice()+i.getProductCategory()+i.getNumber().toLowerCase();
if(productName.contains(jTextField1.getText().toLowerCase()));
{
model.addElement(i.getProductName()+i.getPrice()+i.getProductCategory()+i.getNumber());
}
}
model=filteredProduct;
jList1.setModel(model);
}
Sorry, if the answer is way too obvious, I have just getting started with Java.抱歉,如果答案太明显,我刚刚开始使用 Java。
could be:可能:
String productName= (i.getProductName()+i.getPrice()+i.getProductCategory()+i.getNumber()).toLowerCase();
Best to learn how the debugger works in your IDE of choice and step through the algorithm to see why it doesn't match.最好了解调试器如何在您选择的 IDE 中工作,并逐步了解算法以查看其不匹配的原因。
Edit: To be more clear: Call toLowerCase
on the result of the concatenation, not just the last part.编辑:更清楚一点:在连接的结果上调用
toLowerCase
,而不仅仅是最后一部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.