Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Click event on jTable -Java [duplicate]

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.

like image 509
Harshveer Singh Avatar asked Sep 08 '11 15:09

Harshveer Singh


2 Answers

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) {
            ......

        }
    }
});
like image 121
Costis Aivalis Avatar answered Sep 28 '22 08:09

Costis Aivalis


JTable can listnening for selected TableCell (by mouseclick or from keyboard),you have to look for implemets ListSelectionListener, examples here or here

like image 22
mKorbel Avatar answered Sep 28 '22 07:09

mKorbel