Java: how to select only one cell in jtable and not the whole row

in jTable, I want when the user clicks on a cell, this sentence will be printed on the screen:

I am cell in row X and column Y

where x and Y are the row and column of the clicked cell. But I get: when I click, for example, the cell in row 1 and column 4 I get the following:

I am cell in row 1 and column 0
I am cell in row 1 and column 1
I am cell in row 1 and column 2
....
I am cell in row 1 and column N  ( N = number of columns)

i.e. the entire row is selected.

this is the code:

public class CustomTableCellRenderer extends DefaultTableCellRenderer{

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{

    Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

            if(isSelected) System.out.println("I am cell in row "+row+" and column "+column);



    return cell;

}

}

Thanks for any help.

+5
source share
5 answers

CellRenderers are used to display the contents of a cell. If you want to find the cell that the mouse clicked on, use MouseListener and find the cell in the mouseClicked method.

+1
source

.

( setCellSelectionEnabled(true)), ( getSelectionModel()) . , , getSelectedRow() getSelectedColumn(), , .

, , . , , . KDM.

+14
myTable.setRowSelectionAllowed(false);
+6

if(isSelected) if (isSelected && hasFocus). , .

mKorbel ...

0

myTable.setCellSelectionEnabled();

0

All Articles