Click event on jTable -Java

Harshveer Singh picture Harshveer Singh · Sep 8, 2011 · Viewed 73.1k times · Source

I have created a table in java in Netbeans and filled it with some data. Now I want to show some detail in a text area corresponding to the particular column in a row when I click on that cell. How can I find out using event listener that on which cell user has clicked.

Answer

Costis Aivalis picture Costis Aivalis · Sep 8, 2011

Find the location of the click event and get the cell you are searching for:

jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
    @Override
    public void mouseClicked(java.awt.event.MouseEvent evt) {
        int row = jTable1.rowAtPoint(evt.getPoint());
        int col = jTable1.columnAtPoint(evt.getPoint());
        if (row >= 0 && col >= 0) {
            ......

        }
    }
});