I'm do program that when i press by mouse in any cell in table that print the cell position in a text field but i have a problem that's do nothing need help plz
note ( I'm use jfram form and drage in it jtable and jtextfield )

public void mm()
   {
   jTable1.addMouseListener(new my());

   }
   public class my implements MouseListener{
    public void mouseClicked(MouseEvent e) {

 if (jTable1.getCellSelectionEnabled()) {
      jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
      int rowIndex = jTable1.getSelectedRow();
      int colIndex = jTable1.getSelectedColumn();
      jTextField1.setText(String.valueOf(rowIndex + colIndex));

    }
    }
    public void mousePressed(MouseEvent e) {

    }

    public void mouseReleased(MouseEvent e) {

    }

    public void mouseEntered(MouseEvent e) {

    }

    public void mouseExited(MouseEvent e) {

    }

}

Recommended Answers

All 2 Replies

Use following code:

public void mouseClicked(MouseEvent e) 
{
     if (jTable1.getRowCount()>0) 
     {
          jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
          int rowIndex = jTable1.getSelectedRow();
          int colIndex = jTable1.getSelectedColumn();
          jTextField1.setText(jTable1.getValueAt(rowIndex,colIndex));
     }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.