简体   繁体   English

JTable输入关键动作

[英]JTable enter key actions

I am developing an application using JTable for inventory management. 我正在使用JTable用于库存管理的应用程序。

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 . 操作是,通过在JTextField键入商品代码并按Enter键,该代码的详细信息应显示在JTable And there I have to type the quantity and press enter to calculate the amount. 在这里,我必须输入数量,然后按Enter键以计算数量。

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. 但是现在通过提供项目代码,详细信息到达JTable ,我可以键入数量,但是通过按Enter键, JTable焦点移到了下一行,并且没有进行任何计算。 I am using MySQL and Java in Netbeans. 我在Netbeans中使用MySQL和Java。

I found some code. 我找到了一些代码。 This code you given earlier for this problem 您先前为这个问题给出的代码

private void createKeybindings(JTable table) {
    table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "Enter");
    table.getActionMap().put("Enter", new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent ae) {

        }
    });
}

But I have no clear idea about this code. 但是我对此代码没有明确的想法。

Thank you... 谢谢...

If you have three columns like Quantity, Price, Amount then you could override the setValueAt(...) method of your TableModel with code like: 如果您具有数量,价格,金额三列,则可以使用以下代码覆盖TableModel的setValueAt(...)方法:

public void setValueAt(Object value, int row, int column)
{
    super.setValueAt(value, row, column);

    if (column == 0 || column == 1)
    {
        TableModel model = (TableModel)e.getSource();
        int quantity = ((Integer)model.getValueAt(row, 0)).intValue();
        double price = ((Double)model.getValueAt(row, 1)).doubleValue();
        Double value = new Double(quantity * price);
        model.setValueAt(value, row, 2);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM