gpt4 book ai didi

java - 自定义单元格渲染器

转载 作者:太空宇宙 更新时间:2023-11-04 14:01:23 25 4
gpt4 key购买 nike

是否可以创建自定义单元格渲染器的实例,并根据给定的参数为每列放入不同的值?

例如:

Calendar

我有这个渲染器(MyCustomCellRenderer myRen = new MyCustomCellRenderer),并且对于每一列,我都放置了不同的参数以在ComboBox中显示不同的内容:

MyCustomCellRenderer myRen = new MyCustomCellRenderer
for (int i = 0; i < table.nbColumns; i++) {
myRen.setArg(myArg); //denpending on this argument I fill the combo
myJTable.getColumnModel().getColumn(i).setCellRenderer(myRen);
}

我该怎么做?

编辑:

看起来有一个误解,我有我的参数列表,并且我编写了上面的代码作为示例。

我的问题是:我应该在 MyCustomCellRenderer 类中编写什么以使渲染器每次针对每列进行更改?
我的渲染器类:

public class MyCustomCellRenderer extends JPanel{
private static final long serialVersionUID = 1L;

private JPanel topPanel;
private JTextField hs = new JTextField();
private Activity aObj = new Activity();
@SuppressWarnings({ "unchecked", "rawtypes" })
private JComboBox<ComboItem> activityCombo = new JComboBox(aObj.getListActivitiesForComboBox());
private JComboBox<ComboItem> chantier = new JComboBox<ComboItem>();
private List<Project> listProjectsForChef;
private List<Date> datePointage = new ArrayList<Date>();
private Date columnDate;


public MyCustomCellRenderer(boolean enabled, int idChef) {

this.setLayout(new BorderLayout());
if (enabled) {
Project p = new Project();
p.setChef(idChef);
listProjectsForChef = p.getListProjectsForSelectedChef();
topPanel = new JPanel();
Color color = new Color(128, 128, 128);
chantier.setBorder(BorderFactory.createLineBorder(Color.WHITE));
chantier.setPreferredSize(new Dimension(this.getWidth(), 30));
Services.setSideBorderColor(chantier, "TOP", color);
this.add(chantier, BorderLayout.SOUTH);
topPanel.setLayout(new GridLayout());

hs.setBorder(BorderFactory.createLineBorder(Color.WHITE));
hs.setHorizontalAlignment(JTextField.CENTER);
Services.setSideBorderColor(hs, "RIGHT", color);
hs.setPreferredSize(new Dimension(topPanel.getWidth(), topPanel.getHeight()));
topPanel.add(hs);
topPanel.add(activityCombo);
this.add(topPanel, BorderLayout.CENTER);
}
}
//The function that will take the argument
public void setDatePointage(Date d){
this.datePointage.add(d);
this.columnDate = d;
fillProjectCombobox();//--fill the combo based on the given argument
}

public void fillProjectCombobox() {
this.chantier.removeAllItems();
int nbIde = listProjectsForChef.size();
for (int i = 0; i < nbIde; i++) {
String key = Integer.toString(listProjectsForChef.get(i).getId());
String value = listProjectsForChef.get(i).getDesignation();
java.sql.Date dateEndMappingChiefProject = listProjectsForChef.get(i).getDateFin();
if(dateEndMappingChiefProject != null){
int compare = dateEndMappingChiefProject.compareTo(this.columnDate);
if(compare == 1){//---dateEndMappingChiefProject > this.columnDate
this.chantier.addItem(new ComboItem(key, value));// ---on ajout les
}
}else{
this.chantier.addItem(new ComboItem(key, value));// ---on ajout les
}

}
}
}

最佳答案

您的问题是,您对所有列使用相同的 MyCustomCellRender 实例。这将导致每一列使用相同的渲染器,并以最后一个 myArg 作为参数来渲染您的内容。可能有帮助的是为每列创建一个新的 MyCustomCellRenderer 实例。

例如:

for (int i = 0; i < table.nbColumns; i++) {
MyCustomCellRenderer myRen = new MyCustomCellRenderer //now each column will use it's own renderer instance
//also: don't forget to set myArg
myRen.setArg(myArg);
myJTable.getColumnModel().getColumn(i).setCellRenderer(myRen);
}

关于java - 自定义单元格渲染器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29274869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com