我正在编写一个自定义 POS 系统,并允许用户直接在桌子上更改数量,而不是生成一个对话框。这是表格的第一行,其中包含虚拟数据。(我还没有将它链接到数据库。)
Price 和 Amt 都有自定义单元格渲染器(不是任何人都可以分辨,因为它们渲染不正确)来显示货币格式。编辑:我取消了自定义编辑器(不是渲染器,谢谢),下面的问题仍然存在
计划是当用户在数量框中输入数量时,它会更新 amt 单元格(以及屏幕顶部的运行总计)。为此,我创建了一个验证器,它将验证数据是否为整数,并将其附加到自定义单元格编辑器,该编辑器只是将验证器添加到 JTextField。
我的验证器嵌套在屏幕本身中,因此它可以访问屏幕的字段和表格本身。我想把它放到表格模型中,但我发现我无法分辨用户选择了哪个单元格。(验证()有效,所以我省略了)
class QtyVerifier extends InputVerifier
{
private int qty = 0;
private BigDecimal pricePerUnit;
private BigDecimal prevamt;
@Override
public boolean shouldYieldFocus(JComponent input)
{
//reset quantity
this.qty = 0;
//verify the results
boolean ok2go = this.verify(input);
if (ok2go)
{
//grab all the current values
this.qty = new Integer(
(String)salesOrderFormTable.getValueAt(rowselected, colselected));
this.pricePerUnit = new BigDecimal(
(String)salesOrderFormTable.getValueAt(rowselected, colselected));
this.prevamt = new BigDecimal(
(String)salesOrderFormTable.getValueAt(rowselected, colselected));
//remove previous amount from the total
addLineCostToRunningTotal(this.prevamt.negate());
//update sales order total
addLineCostToRunningTotal(amt);
//update line total cell
salesOrderFormTable.setValueAt(actualTotal, rowselected, 6);
salesOrderFormTable.validate();
}
else { ... }
return ok2go;
}
....
};
这就是它变得非常奇怪的地方。我告诉 qty 数量仍然是 1。它从单元格中提取所有正确的数据。
Selected: 0,1
Quantity in cell 0,1 is 1
Line price is 1.0
Previous amt is 1.0
New amt is 1.0
好的。因此,我将值更改为 5,然后按 Enter。
它将值更改为 25(解决了这个问题)并且还从单元格中提取了错误的数据。
Quantity in cell 0,1 is 5 //this is correct
Line price is 5.0 //this is incorrect. It should be 1.
Previous amt is 5.0 //also incorrect. This also should be 1.
New amt is 25.0 //this WOULD be correct if the previous data was.
我的桌子怎么了?!为什么这会在两个单元格中给我完全错误的信息,并将 qty 单元格更改为我没有输入的内容?有没有更简单的方法来做到这一点(有或没有验证者)?我不能在订单上出现错误的成本。我考虑将我的所有值从双精度数更改为 BigDecimals 以防止舍入错误,但这甚至不是舍入错误。这完全是错误的。我现在完全迷路了。