与 JTable 中的单元格渲染器交互
有没有办法让单元格渲染器响应鼠标事件,例如鼠标悬停?
Is there any way to get a cell renderer to respond to mouse events, such as mouseovers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
有没有办法让单元格渲染器响应鼠标事件,例如鼠标悬停?
Is there any way to get a cell renderer to respond to mouse events, such as mouseovers?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(2)
从未尝试过,但我想您需要:
a)创建一个自定义渲染器以在两种状态下绘制单元格
b)您需要跟踪当前应在“鼠标悬停”状态下绘制哪个单元格
c)添加鼠标监听器跟踪鼠标进入/退出和鼠标移动。对于每个事件,您需要更新一个变量来跟踪鼠标所在的单元格。你
可以使用 JTable 的 columnAtPoint() 和 rowAtPoint() 方法
d) 当鼠标离开单元格时,您需要在单元格上调用 repaint() 。您可以使用 getCellRect() 方法来确定要重绘哪个单元格
e) 当鼠标进入某个单元格时,您需要重置“鼠标悬停”状态的单元格值,然后重新绘制该单元格。
Never tried it but I guess you would need to:
a) create a custom renderer to paint the cell in two states
b) you need to keep track of which cell should currently be painted in the "mouse over" state
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) 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) when the mouse enters a cell you need to reset the cell value for the "mouse over" state and then repaint the cell.
好的,所以我尝试实现 camickr 的方法,但在这个过程中我遇到了一些非常讨厌的问题。我已向 JTable 添加了一个 MouseMotionListener 来跟踪当前和上一个单元格,并添加了一些方法来指示渲染器返回哪个组件,然后重新绘制相应的单元格。然而,由于某种奇怪的原因,即使只有一个重绘请求,每个单元格也会重绘两次。基本上,我能够在鼠标悬停时突出显示单元格,但是一旦鼠标光标离开单元格,就无法从单元格中删除突出显示。经过最初的困惑后,我决定以不同的方式来做这件事。我添加了一个方法,当鼠标悬停在单元格上时调用编辑器,然后添加一些代码,一旦 JToggleButton(我的渲染和编辑组件)的状态发生更改,这些代码就会停止编辑。我当前的代码如下所示:
我希望这会对某人有所帮助
Ok, so I've tried implement camickr's approach, but I've encountered some very nasty problem in the process. I've added a MouseMotionListener to JTable to track the current and previous cell and added some methods that instruct a renderer about which component to return and then repaint the appropriate cell. However, for some odd reason each cell is repainted twice even though there was only one repaint request. Basically, I was able to highlight a cell on a mouse rollover, but it was impossible to remove highlighting from a cell once the mouse cursor leaved it. After initial confusion I've decided to do this in a different way. I've added a method which invokes the editor whe the mouse is over the cell and then added some code that stops editing as soon as the state of JToggleButton (my rendering and editing component) is changed. My current code looks like this :
I hope that will help someone