简体   繁体   中英

JTable enter key actions

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. I am using MySQL and Java in Netbeans.

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:

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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