多个ListCell渲染器
是否可以在单个类中拥有多个 ListCellRenderer 的实现?
实际上,我的应用程序中有多个 JList,并且每个 JList 都需要不同的 ListCellRenderer。
我可以使用不同的类名来实现 ListCellRenderer 的 Component 方法吗?
例如:如果我有一个名为“MultiColumnCellRenderer”的类,具有 Component 方法的某些实现,而另一个类名为“MultiColumnCellRenderer2”,具有 Component 方法的某些其他实现?
public class MultiColumnCellRenderer extends JPanel implements
ListCellRenderer {
public MultiColumnCellRenderer() {
}
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
// Some implementation of Component Method
super.setEnabled(list.isEnabled());
super.setFont(list.getFont());
return this;
}
}
public class MultiColumnCellRenderer2 extends JPanel implements
ListCellRenderer {
public MultiColumnCellRenderer2() {
}
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
// Some implementation of Component Method
super.setEnabled(list.isEnabled());
super.setFont(list.getFont());
return this;
}
}`
如果我做类似的事情:
list1.setCellRenderer(new MultiColumnCellRenderer());
list2.setCellRenderer(new MultiColumnCellRenderer2());
它不起作用......
我正在为 list1 和 list2 寻找不同的渲染。
我怎样才能实现这个目标
Is it possible to have multiple ListCellRenderer's implementation in a single class?
Actually I have multiple JList's in my application and I would I am in need of different ListCellRenderer's for each.
Can I have different class names for Implementing ListCellRenderer's Component method.
For ex: If I have a class with name "MultiColumnCellRenderer" with some implementation of Component method and another class with name "MultiColumnCellRenderer2" with some other implementation of Component method?
public class MultiColumnCellRenderer extends JPanel implements
ListCellRenderer {
public MultiColumnCellRenderer() {
}
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
// Some implementation of Component Method
super.setEnabled(list.isEnabled());
super.setFont(list.getFont());
return this;
}
}
public class MultiColumnCellRenderer2 extends JPanel implements
ListCellRenderer {
public MultiColumnCellRenderer2() {
}
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
// Some implementation of Component Method
super.setEnabled(list.isEnabled());
super.setFont(list.getFont());
return this;
}
}`
And if I do something like:
list1.setCellRenderer(new MultiColumnCellRenderer());
list2.setCellRenderer(new MultiColumnCellRenderer2());
Its not working out....
I am looking for different rendering for both list1 and list2.
How can I achieve this
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果“拥有”的意思是“使用”,而“类”的意思是“GUI”,那么是的。
这是一个示例:
源
If by 'have' you mean 'use' and if by 'class' you mean 'GUI', then yes.
Here is an example:
Source
这是一个有效的 SSCCE - 也许您可以弄清楚这与您自己的代码有何不同。
Here's an SSCCE that works - maybe you can figure out how this is different from your own code.
还可以安排单个
ListCellRenderer
实现来管理指定的Color
。在下面的变体中,使用darker()
颜色来表示选择。It's also possible to arrange for a single
ListCellRenderer
implementation to manage a specifiedColor
. In the variation below, adarker()
color is used to signify selection.