自定义单元格渲染器

Custom Cell Renderer

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

例如:

我有这个渲染器 (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 class 中写什么以使渲染器每次都为每一列更改?
我的渲染器class:

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
            }

        }
    }
}

目前您正在遍历所有列并始终使用相同的 '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);
}

您的问题是,您对所有列使用相同的 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);
}