[英]Barcode scan in java
我使用条形码扫描仪扫描产品条形码。 JTextfield中将显示12位数字。 getText()
将使用该12位数字查找其对应的项目,并增加项目数量。 我的问题是如何在扫描新项目之前删除JTextfield中的文本。 因此getText()
方法可以采用新的12位数字。 我试过在侦听器中添加setText("")
,我知道这不是正确的方法。
任何帮助将不胜感激,谢谢!
barcodeTextField.getDocument().addDocumentListener(new DocumentListener(){
public void changedUpdate(DocumentEvent e) {
try {
doSomething();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
public void insertUpdate(DocumentEvent e) {
try {
doSomething();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
public void removeUpdate(DocumentEvent e) {
try {
doSomething();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
public void doSomething() throws SQLException{
String itemName="";
for(Item eachItem: results){
if(Long.parseLong(barcodeTextField.getText())==eachItem.getUPC()){
itemName = eachItem.getItemName();
itemTextField.setText(eachItem.getItemName());
//barcodeTextField.setText("");
break;
}
}
for(int i=0;i<model.getRowCount();i++){
if(itemName.equals(model.getValueAt(i, 1).toString())){
System.out.println("Item found");
model.incrementQuantity(i);
}
}
}});
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Attempt to mutate in notification
at javax.swing.text.AbstractDocument.writeLock(Unknown Source)
at javax.swing.text.AbstractDocument.replace(Unknown Source)
at javax.swing.text.JTextComponent.setText(Unknown Source)
我的问题是通过使用DocumentFilter解决的。 谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.