更改 JTable 单元格背景颜色
好吧,这是我上一个问题的后续: JTable:单击按钮时更改单元格背景 我现在可以使用 isSelected 参数更改 JTable 中选定单元格的背景颜色,但我无法弄清楚让单元格渲染器在每次渲染时设置某些单元格背景的逻辑。
基本上我想选择一些单元格,单击按钮,更改所选单元格的背景颜色,并在取消选择单元格后保留该颜色(不影响未选择的单元格)。
这似乎是一个简单的问题,但我完全不知道如何做到这一点。
一如既往,任何意见都会受到赞赏。
Alright this is a follow-up to my last question:
JTable: Changing cell background when a button is clicked
I can now change background color of selected cells in the JTable by using the isSelected parameter, but I can't figure out the logic to get the cell renderer to set the backgrounds of certain cells every time it renders.
Basically I want to selected a few cells, click a button, change the background color of selected cells, and have it keep that color after I deselect the cell (without effecting the unselected cells).
This seems like such a simple problem, but I am absolutely stumped on how to do this.
As always, any input is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将需要存储有关选择了哪些单元格以及所需背景的信息。然后,您的 CellRenderer 在决定背景使用什么颜色时需要参考该信息。
渲染器的基本逻辑:
You will need to store information about which cells are selected and the background that is needed. Then your CellRenderer will need to refer to that information when deciding what color to use for the background.
Basic logic for renderer:
您必须将包含颜色的复杂对象作为单元格值传递。
按下按钮应该更新所选对象的对象颜色(对于您的情况下的所选单元格)。您的渲染器必须使用该值的颜色来填充背景。
更改对象颜色后,调用 table.cellChanged() (不记得方法名称)来触发重绘。
类似的东西
You must pass the complex object, containing the color, as cell value.
Pressing the button should update object's color for selected objects (for selected cells in your case). Your renderer must use this value's color to fill background.
After changing object's color, call table.cellChanged() (don't remember the name of method) to trigger repainting.
Something like that