10

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);
Luna
  • 956
  • 4
  • 15
  • 28

2 Answers2

29

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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thanks for the reply..But i dont know much about key binding..If you dont mind can you please tell me where or how i have to add this code step by step..i am totally confused.. – Luna Feb 02 '12 at 09:25
  • Yes, it's a little confusing at first. I found it helpful to read the tutorials and try a few examples; more above. – trashgod Feb 02 '12 at 13:47
  • Excellent. Don't hesitate to update your question or pose a new one containing your [sscce](http://sscce.org/) as the need arises. – trashgod Feb 08 '12 at 17:01
1

You can use Java 8 lambda functions:

final String tustakmaad = "Solve";
KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
        panel.getTblBelgetarihiliste().getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
            enter, tustakmaad);
        panel.getTblBelgetarihiliste().getActionMap().put(tustakmaad, new DelegateAction(
            ae -> eventMytable_enterkey()));

and DelegateAction class should be like this:

package com.ozpas.entegre.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractAction;

public class DelegateAction extends AbstractAction {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    ActionListener myaction = (ae) -> {
        System.out.println("empty action");
    };

    public DelegateAction(ActionListener customaction) {
        this.myaction = customaction;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        myaction.actionPerformed(e);
    }

    public ActionListener getMyaction() {
        return myaction;
    }

    public void setMyaction(ActionListener myaction) {
        this.myaction = myaction;
    }

}
cxw
  • 16,685
  • 2
  • 45
  • 81
engtuncay
  • 867
  • 10
  • 29