title: How to add a row in a JTable when click focus is out of the Table and <ENTER> key is pressed
body: <p>I am designing one client interaction application in which the requirement is as follows:-</p>
  <ol>
  <li>User had already done editing in the cells.</li>
  <li>On pressing Enter key new row is inserted.</li>
  </ol>
  <p>From the above, if client edit something in a cell and press ENTER key it will make a new row with edit. In other words, when user is out of focus from the cells ENTER key must be used to add a row. And when user has focused one cell and press ENTER it must not add a row.</p>
  <ol>
  <li>On cell focus + ENTER key pressed = do nothing but edit of cell.</li>
  <li>Outside cell focus + ENTER key pressed = New row is added.</li>
  </ol>
  <h2>SimpleTableDemo</h2>
  <pre><code>package com.ajay.test;
  
  /*
   * SimpleTableDemo.java requires no other files.
   */
  
  import java.awt.Dimension;
  import java.awt.GridLayout;
  import java.awt.KeyEventDispatcher;
  import java.awt.KeyboardFocusManager;
  import java.awt.event.KeyEvent;
  import java.awt.event.MouseAdapter;
  import java.awt.event.MouseEvent;
  import java.util.Vector;
  
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  import javax.swing.JScrollPane;
  import javax.swing.JTable;
  import javax.swing.table.DefaultTableModel;
  
  public class SimpleTableDemo extends JPanel {
      private boolean DEBUG = false;
      private JTable table = null;
  
      public SimpleTableDemo() {
          super(new GridLayout(1,0));
  
          String[] columnNames = {&quot;First Name&quot;,
                                  &quot;Last Name&quot;,
                                  &quot;Sport&quot;,
                                  &quot;# of Years&quot;,
                                  &quot;Vegetarian&quot;};
  
          Object[][] data = {
          {&quot;Kathy&quot;, &quot;Smith&quot;,
           &quot;Snowboarding&quot;, new Integer(5), new Boolean(false)},
          {&quot;John&quot;, &quot;Doe&quot;,
           &quot;Rowing&quot;, new Integer(3), new Boolean(true)}};
         
  
          table = new JTable(new javax.swing.table.DefaultTableModel(data, columnNames));
          table.setPreferredScrollableViewportSize(new Dimension(500, 70));
          table.setFillsViewportHeight(true);
  
          if (DEBUG) {
              table.addMouseListener(new MouseAdapter() {
                  public void mouseClicked(MouseEvent e) {
                      printDebugData(table);
                  }
              });
          }
  
          //Create the scroll pane and add the table to it.
          JScrollPane scrollPane = new JScrollPane(table);
          
          KeyboardFocusManager.getCurrentKeyboardFocusManager()
          .addKeyEventDispatcher(new KeyEventDispatcher() {
              @Override
              public boolean dispatchKeyEvent(KeyEvent e) {
               if(e.getID() == KeyEvent.KEY_PRESSED)
               {
                    if(e.getKeyCode() == KeyEvent.VK_ENTER)
                    {
                      addRow();
                    }
               }
               
                return false;
              }
        });
          
          
  
          //Add the scroll pane to this panel.
          add(scrollPane);
      }
      
      private void addRow()
      {
          DefaultTableModel model = (DefaultTableModel)table.getModel();
  
         if (model != null) {
         Vector v = new Vector(1);
         for (int j = 0; j &lt; table.getColumnCount(); j++){
                  v.add(&quot;&quot;);
              }
              model.addRow(v);
              }
                 
      }
  
      private void printDebugData(JTable table) {
          int numRows = table.getRowCount();
          int numCols = table.getColumnCount();
          javax.swing.table.TableModel model = table.getModel();
  
          System.out.println(&quot;Value of data: &quot;);
          for (int i=0; i &lt; numRows; i++) {
              System.out.print(&quot;    row &quot; + i + &quot;:&quot;);
              for (int j=0; j &lt; numCols; j++) {
                  System.out.print(&quot;  &quot; + model.getValueAt(i, j));
              }
              System.out.println();
          }
          System.out.println(&quot;--------------------------&quot;);
      }
  
      /**
       * Create the GUI and show it.  For thread safety,
       * this method should be invoked from the
       * event-dispatching thread.
       */
      private static void createAndShowGUI() {
          //Create and set up the window.
          JFrame frame = new JFrame(&quot;SimpleTableDemo&quot;);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
          //Create and set up the content pane.
          SimpleTableDemo newContentPane = new SimpleTableDemo();
          newContentPane.setOpaque(true); //content panes must be opaque
          frame.setContentPane(newContentPane);
  
          //Display the window.
          frame.pack();
          frame.setVisible(true);
      }
  
      public static void main(String[] args) {
          //Schedule a job for the event-dispatching thread:
          //creating and showing this application's GUI.
          javax.swing.SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                  createAndShowGUI();
              }
          });
      }
  }
  </code></pre>
  <p>Please suggest something to add a row if cell focus is not there. And to edit if cell focus is there.</p>