JTable 可点击列标题
我正在尝试制作一个可点击的列标题(以便每当单击时都会调用一个方法)。
图像链接(因为我还没有 10 个声誉) http:// img156.imageshack.us/img156/5764/clickablecolumn.png
列标题为红色矩形。
到目前为止,我所做的就是每当按下任何列字段(例如 James、Benny-G 和 Rokas 的字段)时都会做出响应。 代码:
public void mouseClicked(MouseEvent e)
{
System.out.println("Mouse clicked");
TableColumnModel cModel = table.getColumnModel();//cModel - column model
int selColumn = cModel.getColumnIndexAtX(e.getX());//gets the selected column by clicked x coordinate
}
I am trying to make a clickable column header (so that a method would be called whenever one's clicked).
link to image (since I don't have 10 reputation yet) http://img156.imageshack.us/img156/5764/clickablecolumn.png
The column header is in red rectangle.
What I've done so far is responding whenever any column field (such as the one with James, Benny-G and Rokas) is pressed.
The code:
public void mouseClicked(MouseEvent e)
{
System.out.println("Mouse clicked");
TableColumnModel cModel = table.getColumnModel();//cModel - column model
int selColumn = cModel.getColumnIndexAtX(e.getX());//gets the selected column by clicked x coordinate
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想要向表头添加鼠标侦听器,该表头由
JTableHeader
表示:You want to add a mouse listener to the table header, which is represented by
JTableHeader
: