Search code examples
javatablecellrenderer

Custom Cell Renderer


Is it possible to create an instance of custom cell renderer and put different values in it, for each column, depending on the arguments given to it ?

For example:

Calendar

I have this renderer (MyCustomCellRenderer myRen = new MyCustomCellRenderer), and for each column I put a different argument to display something difeerent in the 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);
}

How can I do this?

Edit:

Looks like there is a missunderstanding, I have my list of arguments and I wrote the code above as example.

My question is : What should I write in MyCustomCellRenderer class to make the renderer changes everytime for each column ?
My renderer 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
            }

        }
    }
}

Solution

  • Your problem is, that you're using the same instance of MyCustomCellRender for all columns. This will result in every column using the same renderer with the last myArg as parameter to render your stuff. What may help is to create a new instance of MyCustomCellRenderer for each column.

    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);
    }