java:如何只选择一个jtable中的一个单元格而不是整个行 [英] java: how to select only one cell in a jtable and not the whole row

查看:383
本文介绍了java:如何只选择一个jtable中的一个单元格而不是整个行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

I am cell in row X and column Y

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

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 :

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)

即整个行都被选中.

这是代码:

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;

}

}

感谢您的帮助.

推荐答案

CellRenderers用于呈现单元格内容.如果要查找单击鼠标所在的单元格,请使用MouseListener并在mouseClicked方法中找到该单元格.

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.

这篇关于java:如何只选择一个jtable中的一个单元格而不是整个行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆