Hi 
I am trying to do a JTable that once the last cell in the last row is 
edited and the user hits the return key, a new row is inserted at the 
end 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 
row at the end of the table. I am wondering whether this is the right 
way to go about it or is there a better way?

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

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 is the same for RETURN key 
presses.

I have a second question : I want to validate the data entered into a 
textfield in a cell in the JTable and at the moment I am doing the 
validation in StopCellEditing(), is there a better way to do this? 

Thanks in advance
Sinead

_______________________________________________
Swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/swing

Reply via email to