与JTable中的单元格渲染器的交互 [英] Interaction with a cell renderer in JTable

查看:82
本文介绍了与JTable中的单元格渲染器的交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何方法可以使单元格渲染器响应鼠标事件(例如鼠标悬停)?

Is there any way to get a cell renderer to respond to mouse events, such as mouseovers?

推荐答案

从未尝试过,但我想您需要:

Never tried it but I guess you would need to:

a)创建一个自定义渲染器,以两种状态绘制单元格

a) create a custom renderer to paint the cell in two states

b)您需要跟踪当前应在鼠标悬停"状态下绘制哪个单元格

b) you need to keep track of which cell should currently be painted in the "mouse over" state

c)添加鼠标侦听器,以跟踪输入/退出的鼠标和mouseMoved的鼠标.对于每个事件,您都需要更新一个变量,以跟踪鼠标置于哪个单元格上.你 可以使用JTable的columnAtPoint()和rowAtPoint()方法

c) add a mouse listener to track mouse entered/exited and mouseMoved. With each event you would need to update a variable that tracks which cell the mouse is positioned over. You can use the columnAtPoint() and rowAtPoint() methods of JTable

d)当鼠标离开一个单元格时,您需要在该单元格上调用repaint().您可以使用getCellRect()方法来确定要重绘的单元格

d) when the mouse leaves a cell you need to invoke repaint() on the cell. You can use the getCellRect() method to determine which cell to repaint

e)当鼠标进入一个单元格时,您需要将单元格值重置为鼠标悬停"状态,然后重新绘制该单元格.

e) when the mouse enters a cell you need to reset the cell value for the "mouse over" state and then repaint the cell.

这篇关于与JTable中的单元格渲染器的交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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