I'm coding a JTable that must have the capability to do the following;
-the final cell in the bottom row has been edited and the user hits
the return key thereby causing a new row to be inserted below this
edited rpw, at the bottom of the table. I have created a listener
object in the JTable which catches a keyEvent and if it is a ENTER
key event, inserts a new row at the bottom of the table.
Is the right way to go about it or is there a better way? (see code
below).I have come across some stuff about the TAB key and how Swing
components filter out TAB key presses at a very low level and pass
them to the Swing focus manager, where they are consumed(Core Swing
advanced Programming:kim Topley). Is it the same for RETURN key
presses?
Also, I want to validate the data entered into a textfield in a cell
in the JTable -I'm doing the validation in StopCellEditing(), is
there a better way to do this?
/**
* As each key is passed to the editor
* component in editCellAt() the keylistener
* implemented in this class is notified.
*/
public class ReturnKeyListener extends KeyAdapter{
/**
* This method checks whether the key is a
* RETURN/ENTER key and if it is,
* the current edit is stopped by invoking
* the editors stopCellEditing() method.
* If this works, the editor has been removed
* and the current row count is gotten
* and if the editing row is the last
* row in the table, insertRow() is called
*/
public void keyPressed(KeyEvent evt){
if(evt.getKeyCode() == KeyEvent.VK_ENTER){
if(inReturnEditor == true){
TableCellEditor editor = getCellEditor();
int editRow = getEditingRow();
int editCol = getEditingColumn();
if (editor != null){
boolean stopped = editor.stopCellEditing();
if(stopped == true){
int rowCount = getRowCount();
if(editRow == (rowCount - 1)){
insertRow(editRow + 1);
}
}
}
}
}
}
}
Thanks
_______________________________________________
Advanced-swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/advanced-swing