0

I'm having a strange problem with my custom TableCellRenderer for Checkbox rendering, which works correctly on the first line but on the others, it sets un/selected all the checkboxes between first click and second one. Here's the code :

/*This Class is meant to be the Concrete Aspect*
*              of CheckBox in JTable           */

public class CheckItem{
private String label;
private boolean isSelected=false;

public CheckItem(String label){
    this.label=label;
}

public boolean isSelected(){
    return isSelected;
}

public void setSelected(boolean isSelected){
    this.isSelected=isSelected;
}

@Override
public String toString(){
    return label;
}
}

and now the Renderer I use :

/*This Class is a renderer simulating a CheckBox in JTable Column(s)*/

public class CheckTableRenderer extends JCheckBox
    implements TableCellRenderer{

@Override
public Component getTableCellRendererComponent(JTable table,
        Object value, boolean isSelected, boolean hasFocus, 
        int row, int column) {
    if(value==null){
        return null;
    }

    if (isSelected) {
            setForeground(table.getSelectionForeground());
            setBackground(table.getSelectionBackground());
    } 
    else {
            setForeground(table.getForeground());
            setBackground(table.getBackground());
    }

    CheckItem prov=(CheckItem)value;

    if(!prov.toString().equals("")){
        this.setText(prov.toString());
    }       
    else 
        setHorizontalAlignment(CENTER);

    this.setSelected(prov.isSelected());

    return this;
} 
}

and finally the mouseListener to change the value, contained in class Listeners :

public static MouseListener createCheckMouseListener(final JTable table,final int column){

    MouseListener m=new MouseAdapter() {
        public void mouseClicked(MouseEvent e){
            final int x=table.getSelectedRow();
            final int y=table.getSelectedColumn();

            if((y==column || column==-1)&& y==table.columnAtPoint(e.getPoint())){
                try{
                    CheckItem item=(CheckItem)table.getValueAt(x,y);
                    item.setSelected(!item.isSelected());
                    table.repaint(table.getCellRect(x,y,false));
                }catch(java.lang.NullPointerException e2){
                    System.out.println("ERREUR!!!!");
                }
            }
        }
    };

    return m;
}

and its association : mytable.addMouseListener(Listeners.createCheckMouseListener);

All this code seems to be correct but the result is not good concerning rows, first one excepted. It works using Boolean instead of CheckItem but I need to put a String in the checkbox. I can't fix it and I'm even wondering if the problem is located in these parts... Can you help me?

Thanks.

Here's an example : SSCCE

  • What exactly is the problem? I can spot 2 bugs, but I'm not sure I spot all of them, and having a description of the problem would help. – JB Nizet Aug 23 '13 at 11:00
  • 1
    he means, 1st. for better help sooner post an [SSCCE](http://sscce.org/), short, runnable, compilable, and 2nd. for why reason you refused usage of built_in support for JCheckBox as XxxTableCellRenderer/Editor – mKorbel Aug 23 '13 at 11:33
  • Hum as I sais I need a checkbox with String field like in CheckItem and Boolean can't be extended because final class. My problem is that there's only the first checkbox working correctly. All the others change the render of many others checkbox (in the same column) without any reason. The bug is from rendering because the events are well called and when i scroll, checkboxes become checked whereas I even don't click on it. – user1888438 Aug 23 '13 at 12:05
  • Here's an example : http://speedy.sh/4CSbf/SSCE.jar – user1888438 Aug 23 '13 at 12:44
  • 1
    Please edit your question to include your [sscce](http://sscce.org/). – trashgod Aug 23 '13 at 14:22
  • I already did but I can't give just a code block to be executed because of many dependances and other things...The jar file is secure, it's just a table use sandbox if u don't trust – user1888438 Aug 23 '13 at 14:26
  • 1
    `I can't give just a code block to be executed because of many dependances` - Exactly. You need to take 5 minutes to create a proper SSCCE. Your question is about a table with a custom renderer. Remove all the other dependencies and post your code. Most time when you do this you will find the problem. – camickr Aug 23 '13 at 15:05

0 Answers0