JTable输入密钥 [英] JTable enter key
问题描述
我正在使用 jTable
开发一个用于库存管理的应用程序。
I am developing an application using jTable
for inventory management.
通过输入项目,操作是 jTextField
中的代码,按 Enter 键,该代码的详细信息应该是 jTable
。在那里我必须输入数量并按 Enter 来计算金额。但现在通过提供商品代码,详细信息将转到 jTable
,我可以输入数量,但按 Enter key jTable
焦点转到下一行,不进行任何计算。但是,再次在 jTextField
上按 Enter 键,将计算最后输入的金额。我不知道如何解决这个问题,因为我是Java的初学者。我在Netbeans中使用MySQL和Java。
The action is, by typing the item code in a jTextField
and by pressing Enter key, the details of that code should come to jTable
. And there I have to type the quantity and press Enter to calculate the amount. But now by giving item code the details come to the jTable
, and I can type the quantity, but there by pressing Enter key jTable
focus goes to the next row and no calculation is being done. But by again pressing Enter key on the jTextField
the last entered amount is getting calculated. I don't know how to solve this problem as I am a beginner in Java. I am using MySQL and Java in Netbeans.
我在下面给出了这个代码..
I am giving that code below..
谢谢。 。
jTable1.editCellAt(serialNumber, 2);
jTable1.getCellSelectionEnabled();
value1 = new Double(jTable1.getValueAt(serialNumber, 2).toString());
value = new Double(jTable1.getValueAt(serialNumber, 3).toString());
double result = value1 * value;
jTable1.setValueAt(result, serialNumber, 4);
推荐答案
默认 Enter 的docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html\"rel =noreferrer> 键绑定 是 selectNextRowCell
表格的 WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
输入地图中的操作。您可以替换自己的操作,如下所述。
The default Key Binding for Enter is the selectNextRowCell
action in the table's WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
input map. You can substitute your own action, as outlined below.
private static final String solve = "Solve";
KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(enter, solve);
table.getActionMap().put(solve, new EnterAction());
...
private class EnterAction extends AbstractAction {
@Override
public void actionPerformed(ActionEvent e) {
...
}
}
另请参阅 Swing中的键盘绑定 (在web.archive.org上反映)。
See also Keyboard Bindings in Swing (mirrored at web.archive.org).
附录:你可以找到更多的例子此处,此处和这里;最后一个是 JTable
具体。
Addendum: You can find more examples here, here and here; the last one is JTable
specific.
这篇关于JTable输入密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!