是否可以创建自定义单元格渲染器的实例,并为每列添加不同的值,具体取决于赋予它的参数?
例如:
我有这个渲染器(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
}
}
}
}
答案 0 :(得分:0)
目前,您正在遍历所有列并始终使用相同的“myarg”。有很多方法可以做到这一点(很难在没有看到代码的情况下告诉你最好的方法)但是你的方法可以采用额外的参数,比如myarg2
,然后使用if语句(检查i == 1):
MyCustomCellRenderer myRen = new MyCustomCellRenderer
for(int i = 0; i<table.nbColumns; i++){
if(i==1){
myRen.setArg(myArg2);// changed to myArg2
myJTable.getColumnModel().getColumn(i).setCellRenderer(myRen);
continue;
}
myRen.setArg(myArg);//----denpending on this argument I fill the combo
myJTable.getColumnModel().getColumn(i).setCellRenderer(myRen);
}
答案 1 :(得分:0)
您的问题是,您为所有列使用了相同的 MyCustomCellRender 实例。这将导致每列使用相同的渲染器和最后的 myArg 作为参数来渲染您的东西。可能有帮助的是为每列创建一个新的MyCustomCellRenderer实例。
E.g:
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);
}