Tuesday, October 11, 2011

JTable KeyListener

First create a JFrame and add JTable with data. Create the following class. Below the JFrame's class
class jTableController implements KeyListener {

JTable table1;

public jTableController(JTable table) {
table1 = table;
}

//if you want to get selected data
private int[] getSelectedCell() {
int[] xy = new int[2];
xy[0] = table1.getSelectedRow();
xy[1] = table1.getSelectedColumn();
return xy;
}

@Override
public void keyPressed(KeyEvent e) {
System.out.println("keyPressed");
}

public void keyTyped(KeyEvent e) {
System.out.println("keyTyped");
}

public void keyReleased(KeyEvent e) {
System.out.println("keyReleased");
}
}
Inside JFrame's constructor add the following code.
       
tblMaterialSupplierDetails.addKeyListener(new jTableController(tblMaterialSupplierDetails));

That's it run the project and see.

No comments:

Post a Comment