禁用 JList 中的项目
我使用 JList 作为向导的一部分来显示要执行的所有步骤(它还允许单击某个步骤即可转到该步骤)。根据前面步骤中所做的操作,某些步骤并不总是需要的。我想在列表中禁用这些不适用的步骤。
如何禁用(阻止选择)列表中的某些项目?有没有比继承 JList 并重写每个与选择相关的方法更好的方法?
I'm using a JList as part of a wizard to display all the steps to be performed (it also allows clicking on a step to go to it). Some steps will not always be needed, based on what's done in previous steps. It's these inapplicable steps I'd like to disable in the list.
How can I go about disabling (preventing the selection of) certain items in the list? Is there a better way than subclassing JList and overriding every selection-related method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你必须实现DefaultListSelectionModel,然后你可以设置标志是否启用
简单示例
you have to implements DefaultListSelectionModel, then you can set the Flag if isEnabled or not
simple example
如果您从列表模型中删除不适用的项目,而不是禁用它们,会更容易。用户真的想看到禁用的项目吗?我不认为它增加了任何价值,反而增加了视觉混乱。
It would be easier if you removed the inapplicable items from the list model, instead of disabling them. Would users really want to see disabled items? I don't believe it adds any value, but instead adds visual clutter.
我想要一个包含无法选择且透明的单元格的 JList。这就是我所做的:
I wanted a JList with cells that couldn't be select AND were transparent. So here's what I did:
一种解决方案:您可能想要在 Java API 中查找 AbstractAction。然后,实现命令模式。
这个想法是通过将方法变成类(实现 AbstractAction)来赋予方法状态。然后,他们可以互相禁用。
http://en.wikipedia.org/wiki/Command_pattern
http://docs.oracle.com/javase/6/docs/api/
One solution: You might want to look up AbstractAction in the Java API. Then, implement the COMMAND pattern.
The idea is to give methods states by making them into classes (which implement AbstractAction). Then, they can disable each other.
http://en.wikipedia.org/wiki/Command_pattern
http://docs.oracle.com/javase/6/docs/api/