Java 将ListCellRenderer应用于JList上的单个单元格
是否可以将Java 将ListCellRenderer应用于JList上的单个单元格,java,swing,jlist,listcellrenderer,Java,Swing,Jlist,Listcellrenderer,是否可以将listcellrenderer应用于jlist中的一个单元格?我的代码目前可以很好地应用渲染器,但我想为每个条目设置不同的动态变量。如果这有点含糊,我道歉 总之,我只想对列表中的一个单元格应用listcellrenderer,我该怎么做 是否可以将listcellrenderer仅应用于jlist中的一个单元格 不,所有单元都必须共享同一个渲染器。渲染器就是这样工作的 我的代码目前可以很好地应用渲染器,但我想为每个条目设置不同的动态变量 这是可以做到的。渲染器可以根据要渲染的数据的状
listcellrenderer
应用于jlist
中的一个单元格?我的代码目前可以很好地应用渲染器,但我想为每个条目设置不同的动态变量。如果这有点含糊,我道歉
总之,我只想对列表中的一个单元格应用listcellrenderer
,我该怎么做
是否可以将listcellrenderer仅应用于jlist中的一个单元格
不,所有单元都必须共享同一个渲染器。渲染器就是这样工作的
我的代码目前可以很好地应用渲染器,但我想为每个条目设置不同的动态变量
这是可以做到的。渲染器可以根据要渲染的数据的状态更改其渲染单元格的方式
如果这有点含糊,我道歉
如果你能多解释,多展示代码,那就更好了
总之,我只想将listcellrenderer应用于列表中的一个单元格,我该怎么做
同样,渲染器的行为取决于单元格所持有的值。对于更详细的答案,请考虑创建和发布一个并解释更多(例如,呈现不同的方式?)
是否可以将listcellrenderer仅应用于jlist中的一个单元格
不,所有单元都必须共享同一个渲染器。渲染器就是这样工作的
我的代码目前可以很好地应用渲染器,但我想为每个条目设置不同的动态变量
这是可以做到的。渲染器可以根据要渲染的数据的状态更改其渲染单元格的方式
如果这有点含糊,我道歉
如果你能多解释,多展示代码,那就更好了
总之,我只想将listcellrenderer应用于列表中的一个单元格,我该怎么做
同样,渲染器的行为取决于单元格所持有的值。有关更详细的答案,请考虑创建和发布一个并解释更多(例如,以不同的方式呈现)。< p>您必须将列表池渲染器应用到列表中的所有元素,但这并不意味着它必须以相同的方式呈现所有这些元素。例如,可以基于单元格的值(原始值或仅基于值的类,或甚至基于单元格的索引)渲染单元格:
package com.example;
import java.awt.Color;
import java.awt.Component;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
public class ListCellRendererExample extends JFrame {
public ListCellRendererExample() {
DefaultListModel model = new DefaultListModel();
model.addElement(Color.BLUE);
model.addElement("hello");
model.addElement(5);
model.addElement(Color.RED);
JList jlist = new JList(model);
jlist.setCellRenderer(new SuperDuperListCellRenderer());
add(new JScrollPane(jlist));
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setLocationByPlatform(true);
setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
new ListCellRendererExample();
}
private static class SuperDuperListCellRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
// If the value is a color, give the cell a blank value but save its
// value so we can later change its background to the value's color.
Color bgColor = null;
if (value instanceof Color) {
bgColor = (Color) value;
value = " ";
}
// Prepend the index to the "even" rows (the first row is row 1)
if ((index + 1) % 2 == 0) {
value = index + ": " + value;
}
Component renderComponent = super.getListCellRendererComponent(
list, value, index, isSelected, cellHasFocus);
// If the value is a color, set the cell's background to that color.
if (bgColor != null) {
renderComponent.setBackground(bgColor);
}
return renderComponent;
}
}
}
您必须将ListCellRenderer应用于列表中的所有元素,但这并不意味着它必须以相同的方式渲染所有元素。例如,您可以基于其值(原始值,甚至仅基于值的类,甚至基于单元格的索引)渲染单元格:
package com.example;
import java.awt.Color;
import java.awt.Component;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
public class ListCellRendererExample extends JFrame {
public ListCellRendererExample() {
DefaultListModel model = new DefaultListModel();
model.addElement(Color.BLUE);
model.addElement("hello");
model.addElement(5);
model.addElement(Color.RED);
JList jlist = new JList(model);
jlist.setCellRenderer(new SuperDuperListCellRenderer());
add(new JScrollPane(jlist));
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setLocationByPlatform(true);
setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
new ListCellRendererExample();
}
private static class SuperDuperListCellRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
// If the value is a color, give the cell a blank value but save its
// value so we can later change its background to the value's color.
Color bgColor = null;
if (value instanceof Color) {
bgColor = (Color) value;
value = " ";
}
// Prepend the index to the "even" rows (the first row is row 1)
if ((index + 1) % 2 == 0) {
value = index + ": " + value;
}
Component renderComponent = super.getListCellRendererComponent(
list, value, index, isSelected, cellHasFocus);
// If the value is a color, set the cell's background to that color.
if (bgColor != null) {
renderComponent.setBackground(bgColor);
}
return renderComponent;
}
}
}
非常感谢,我将发布代码并对问题进行更详细的解释!如果您有任何想法,请点击这里链接问题!@iainmac编辑您的现有问题以包含示例而不是创建新问题是一种常见做法。非常感谢,我将发布代码并对问题进行更详细的解释问题!这里是问题的链接,如果你有什么好想法的话!@iainmac通常会编辑你现有的问题以包括示例,而不是创建新问题。