title: MouseClicked not triggered for JTable in a GUI with both Action and Mouse Listener
body: <p>I have a GUI in java swing with a JTable and JButtons that implements both ActionListener and MouseListener. I use both because ActionListener cannot be registered for JTable.  If I select a row of the JTable  and press the JButton "Delete", a row must be deleted. This works only for the first time I press delete.  The next time I select a row, mouseListener does not seem to be triggered. I print the selected row number whenever I click on the JTable.  If I consecutively click on the JTable the row gets printed correctly.  But if I press one of the buttons(that trigger ActionPerformed) and then JTable mouseClicked is not called. What is wrong here??</p>
  
  <p>Thanks in Advance</p>
  
  <p>Posting the code:</p>
  
  <pre><code>private void createTable(){
       tableModel=new SimpleTableModel(row);
       //SimpleTableModel populates the table model from vector
       jTable1 = new JTable(tableModel);
       jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  
       jScrollPane1 = new JScrollPane(jTable1);
       jTable1.addMouseListener(this);
       //Add the scroll pane to this panel.
       add(jScrollPane1);
       this.repaint();
  }
  
  public void mouseClicked(MouseEvent evt) {
      if (SwingUtilities.isLeftMouseButton(evt) &amp;&amp; evt.getSource() == jTable1) {
          selectedRow = jTable1.getSelectedRow();
          ruleId = (String) jTable1.getValueAt(selectedRow, 0);
          System.out.println("You have selected row=" + selectedRow
                  + " with ruleId=" + ruleId);
      }
  }
  </code></pre>
  
  <p>More code (suggested by kleopatra - i just added it inside a method doAction() with some changes):</p>
  
  <pre><code>private void doAction() {
      Action delete = new AbstractAction("Delete Selected Row") {
  
          @Override
          public void actionPerformed(ActionEvent e) {
              /*
               * if (jTable1.getSelectedRow() &lt; 0) return; int modelRowIndex =
               * jTable1.convertRowIndexToModel(jTable1.getSelectedRow());
               * ((DefaultTableModel)
               * jTable1.getModel()).removeRow(modelRowIndex);
               * 
               * Since I use a tableModel (which is a class named
               * SimpleTableModel) that inherits AbstractTableModel,
               * ClassCastException popped up. So I used normal code.
               */
              selectedRow = jTable1.getSelectedRow();
              System.out.println("Selected row:" + selectedRow);
              if (selectedRow == -1) {
                  System.out
                          .println("Select a row in the table before clicking \"Delete\"");
                  return;
              }
              row.remove(selectedRow);
              createTable();
          }
      };
      JButton button = new JButton(delete);
  } 
  </code></pre>
  
  <p>Here's the complete code:</p>
  
  <pre><code>public class Data {
  
  
        private String a; 
  
        private String b;
  
        private String c;
  
        private String d;
  
        private String e;
  
  
  
        public Data() {
        }
  
        public Data(String aa, String bb, String cc, String dd,String ee) {
          setA(aa);
          setB(bb);
          setC(cc);
          setD(dd);
          setE(ee);
        }     
  
  
      public void setA(String a) {
          this.a = a;
      }
  
      public String getA() {
          return a;
      }
  
      public void setB(String b) {
          this.b = b;
      }
  
      public String getB() {
          return b;
      }
  
      public void setC(String c) {
          this.c = c;
      }
  
      public String getC() {
          return c;
      }
  
      public void setD(String d) {
          this.d = d;
      }
  
      public String getD() {
          return d;
      }
  
      public void setE(String e) {
          this.e = e;
      }
  
      public String getE() {
          return e;
      }
  }
  
  
  public class SimpleTableModel extends AbstractTableModel {
  public String[] colNames = { "AA","BB","CC","DD","EE"};
  
  
  public Class[] colTypes ={String.class, String.class, String.class,String.class,String.class };
  
  
  Vector dataVector;
  
  
  public SimpleTableModel(Vector dataVector) {
    super();
    this.dataVector = dataVector;
  } 
  
  public int getColumnCount() {
      return colNames.length;
  }
  
  public String getColumnName(int column) {
      return colNames[column];
  }
  
  
      public int getRowCount() {
          return dataVector.size();
        }
  
      @Override
      public Object getValueAt(int rowIndex, int columnIndex) {
          Data macData = (Data) (dataVector.elementAt(rowIndex));
  
            switch (columnIndex) {
            case 0:
              return macData.getA();
            case 1:
              return macData.getB();
            case 2:
              return macData.getC();
            case 3:
              return macData.getD();
            case 4:
                return macData.getE();
  
            }
  
            return new String();
          }
  }
  
  public class DisplayGUI extends JPanel implements ActionListener {
  
  private JTable jTable1;
  private JButton jButton1;
  private JScrollPane jScrollPane1;
  private SimpleTableModel tableModel;
  static Vector&lt;Data&gt; row=new Vector&lt;Data&gt;(10,10);
  static int selectedRow;
  public DisplayGUI(){
      initComponents();
  }
  private void initComponents(){
  
      jButton1 =new JButton();
      jButton1.setText("Delete");
      jButton1.addActionListener(this);
      row.addElement(new Data("abc","bcd","cde","def","efg"));
      row.addElement(new Data("1","2","3","4","5"));
      row.addElement(new Data("10","11","12","13","14"));
      createTable();
      add(jButton1);
  }
  
  
  private void createTable() {
      tableModel=new SimpleTableModel(row);
       jTable1 = new JTable(tableModel);
        jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1 = new JScrollPane(jTable1);
        add(jScrollPane1);
          this.repaint();
  }
  
  
  public static void main(String[] args){
       JFrame frame = new JFrame("DisplayGUI");
          //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
          //Create and set up the content pane.
          DisplayGUI newContentPane = new DisplayGUI();
          newContentPane.setOpaque(true); //content panes must be opaque
          frame.setContentPane(newContentPane);
  
          //Display the window.
          frame.setVisible(true);
  }
  
  public void actionPerformed(ActionEvent evt){
  
      if(evt.getSource()==jButton1){
          selectedRow=jTable1.getSelectedRow();
          System.out.println("You have selected "+selectedRow);
          if(selectedRow==-1){
              JOptionPane.showMessageDialog(this, "Select a row in the 
                                                     table before clicking \"Delete\"",
                      "Alert!",JOptionPane.PLAIN_MESSAGE);
              return;
          }
          row.remove(selectedRow);
          createTable();
      }
  }
  
  }
  </code></pre>
  
  <p>Did the following changes to make my code working:</p>
  
  <pre><code> private void initComponents(){
  
  jButton1 =new JButton();
  jButton1.setText("Delete");
  jButton1.addActionListener(this);
  row.addElement(new Data("abc","bcd","cde","def","efg"));
  row.addElement(new Data("1","2","3","4","5"));
  row.addElement(new Data("10","11","12","13","14"));
  tableModel=new SimpleTableModel(row);
  jTable1 = new JTable(tableModel);
   jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
   jScrollPane1 = new JScrollPane(jTable1);
   add(jScrollPane1);
   add(jButton1);
   createTable();
  
  }
  
  
  private void createTable() {
   tableModel.setDataVector(row);
  //Added a setDataVector method to class Data
   jTable1.setModel(tableModel);
  
      this.repaint();
   }
  
  
  /*Also, added a condition jTable1.getRowCount()==0 to check for empty table before    
  *deleting in actionPerformed.
  */
  </code></pre>