java:如何在jtable中仅选择一个单元格而不是整行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6451200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:03:56  来源:igfitidea点击:

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

javaswingrenderingjtablerow

提问by shaw

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

在 jTable 中,我希望当用户单击一个单元格时,这句话会打印在屏幕上:

I am cell in row X and column Y

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

其中 x 和 Y 是单击单元格的行和列。但是我得到的是:例如,当我单击第 1 行和第 4 列中的单元格时,我得到以下信息:

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 whole 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.

谢谢你的帮助。

采纳答案by Dakshinamurthy Karra

CellRenderers are used for rendering the cell contents. If you want to find the cell in which the mouse clicked, use a MouseListener and in the mouseClicked method find the cell.

CellRenderers 用于渲染单元格内容。如果要查找鼠标单击的单元格,请使用 MouseListener 并在 mouseClicked 方法中查找单元格。

回答by JB Nizet

You shouldn't use a cell renderer for this.

您不应该为此使用单元格渲染器。

Enable cell selection on your table (using setCellSelectionEnabled(true)), then get the selection model of the table (using getSelectionModel()), and add a listener on this selection model. Each time an event is triggered, use getSelectedRow()and getSelectedColumn()to know which cell is selected.

在您的表格上启用单元格选择(使用setCellSelectionEnabled(true)),然后获取表格的选择模型(使用getSelectionModel()),并在此选择模型上添加一个侦听器。每次触发事件时,使用getSelectedRow()getSelectedColumn()了解选择了哪个单元格。

Note that this will give you the selected cell, which can be modified using the mouse or the keyboard. If you just want to know where the mouse clicked, then see KDM's answer.

请注意,这将为您提供选定的单元格,可以使用鼠标或键盘对其进行修改。如果您只想知道鼠标单击的位置,请参阅 KDM 的答案。

回答by mKorbel

myTable.setRowSelectionAllowed(false);

回答by BenCole

Change your if(isSelected)to if (isSelected && hasFocus). This will print for only the selected cell, rather than the selected row.

将您更改if(isSelected)if (isSelected && hasFocus). 这将只打印选定的单元格,而不是选定的行。

mKorbel's answer should also work...

mKorbel 的回答也应该有效......

回答by Guy Levin

myTable.setCellSelectionEnabled(true);

myTable.setCellSelectionEnabled(true);