Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
411 views
in Technique[技术] by (71.8m points)

java - JTable enter key

I am developing an application using jTable for inventory management.

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..

Thank You..

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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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) {
        ...
    }
}

See also Keyboard Bindings in Swing (mirrored at web.archive.org).

Addendum: You can find more examples here, here and here; the last one is JTable specific.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...