如何动态更改JList中所选项目的背景颜色
如何动态更改 JList 中选择的项目的背景颜色?
How can I change the background color of the item which is selected in JList dynamically?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
像下面这样的东西应该有助于作为起点:
Something like the following should help as a starting point:
一种更简单的方法是进入 Eclipse 中的设计模式,然后在 JList 的属性中,单击带有两个小箭头且中间有一个黄色大箭头的按钮,以打开“显示高级属性”。然后向下滚动并更改“selectionBackground”处的颜色并更改那里的颜色(它可能是灰色的,但它仍然会改变)。现在,当您运行程序时,无论您选择什么,背景都将是该颜色。
An easier way would be to go to design mode in Eclipse, and in the properties of your JList, click on the button that has two small arrows with a big yellow arrow inbetween to open up "show advanced properties." then scroll down and change the color where it says "selectionBackground" and change the color there (it will probably be gray, but it will still change). Now, when you run your program, whatever you select, the background will be that color.
只需在循环中设置要着色的所有项目的选定索引并相应地更改颜色即可!
Just Set Selected index of all the items you want to color in a loop and Change the color Accordingly!
如果我清楚地理解你,请查看
javax.swing.ListCellRenderer
。您需要重新实现它或扩展 javax.swing.DefaultListCellRenderer 并自定义 getListCellRendererComponent 方法。
If I am clearly understanding you, look into
javax.swing.ListCellRenderer
.You need to reimplement it or extend
javax.swing.DefaultListCellRenderer
and customize thegetListCellRendererComponent
method.