Hi Every one

I have written a ComboBox which is having the CheckBox options.
I want to render the ComboCheckBox in the table cell.
Please help me out in writing the Tablemodel and Renderer for this.
I am pasting the Code which displays a Frame with ComboCheckBox on the top and the table that renders the ComboCheckBox in the first column,

Please help me out in resolving this issue.




public class CheckComboBox extends JComboBox {
/**
*
*/
private static final long serialVersionUID = 1L;
private List<ObjCheckBox> cbs;
private Map<Object, Boolean> mapObjSelected;
private Object nullObject = new Object();

public CheckComboBox(final Set objs) {
this(objs, false);
}

public CheckComboBox(final Set objs, boolean selected) {
resetObjs(objs, selected);
}

public CheckComboBox(final Set objs, final Set selected) {
mapObjSelected = new LinkedHashMap();
for (Object obj : objs) {
if (obj==null)
obj = nullObject;
mapObjSelected.put(obj, selected.contains(obj));
}
reset();
}

public CheckComboBox(Map<Object, Boolean> mapObjSelected) {
this.mapObjSelected = mapObjSelected;
reset();
}

public void resetObjs(final Set objs, boolean selected) {
mapObjSelected = new LinkedHashMap();
for (Object obj : objs) {
mapObjSelected.put(obj, selected);
}

reset();
}

public Object[] getSelectedItems() {
Set ret = new TreeSet(); // alphabetically
for (Map.Entry<Object,Boolean> entry : mapObjSelected.entrySet()) {
Object obj = entry.getKey();
Boolean selected = entry.getValue();

if (selected) {
ret.add(obj);
}
}

if (ret.isEmpty()) return null;

return ret.toArray(new Object[ret.size()]);
}

public void addSelectedItems(Collection c) {
if (c==null) return;

for (Object obj : c) {
if (mapObjSelected.containsKey(obj)) {
mapObjSelected.put(obj, true);
}
}

reset();
repaint();
}

public void addSelectedItems(Object[] objs) {
if (objs==null) return;

for (Object obj : objs) {
if (mapObjSelected.containsKey(obj)) {
mapObjSelected.put(obj, true);
}
}

reset();
repaint();
}

private void reset() {
this.removeAllItems();

initCBs();

this.addItem(new String());
for (JCheckBox cb : cbs) {
this.addItem(cb);
}

setRenderer(new CheckBoxRenderer(cbs));
addActionListener(this);
}

private void initCBs() {
cbs = new Vector<ObjCheckBox>();

ObjCheckBox cb;
for (Map.Entry<Object,Boolean> entry : mapObjSelected.entrySet()) {
Object obj = entry.getKey();
Boolean selected = entry.getValue();

cb = new ObjCheckBox(obj);
cb.setSelected(selected);
cbs.add(cb);
}

}

private void checkBoxSelectionChanged(int index) {
int n = cbs.size();
if (index<0 || index>=n) return;


if (index<n) {
ObjCheckBox cb = cbs.get(index);


if (cb.isSelected()) {
cb.setSelected(false);
mapObjSelected.put(cb.getObj(), false);

} else {
cb.setSelected(true);
mapObjSelected.put(cb.getObj(), true);
}
}
}

@Override
public void actionPerformed(ActionEvent e) {
int sel = getSelectedIndex();
System.out.println("The selected index"+sel);
if (sel == 0) {
getUI().setPopupVisible(this, false);
} else if (sel > 0) {
checkBoxSelectionChanged(sel-1);
}

this.setSelectedIndex(-1); // clear selection
}

@Override
public void setPopupVisible(boolean flag)
{
//TODO this not work, fix it
// Not code here prevents the populist from closing
}

// checkbox renderer for combobox
class CheckBoxRenderer implements ListCellRenderer {
private final DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer();

private final List<ObjCheckBox> cbs;

public CheckBoxRenderer(final List<ObjCheckBox> cbs) {

this.cbs = cbs;
}

//@Override
public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
if (index > 0 && index <= cbs.size()) {
ObjCheckBox cb = cbs.get(index-1);
/*if (cb.getObj()==nullObject) {
return separator;
}*/

cb.setBackground(isSelected ? Color.blue : Color.white);
cb.setForeground(isSelected ? Color.white : Color.black);

return cb;
}

String str;
Object[] objs = getSelectedItems();
Vector<String> strs = new Vector();
if (objs==null) {
str = "select Option";
} else {
for (Object obj : objs) {
strs.add(obj.toString());
}
str = strs.get(0);
}
return defaultRenderer.getListCellRendererComponent(list, str, index, isSelected, cellHasFocus);
}
}

class ObjCheckBox extends JCheckBox {
private final Object obj;
public ObjCheckBox(final Object obj) {
super(obj.toString());
this.obj = obj;
}

public Object getObj() {
return obj;
}
}

public static void main(String args[])
{
JFrame frame = new JFrame();
JTable table = new JTable(new MyTableModel());

HashSet set = new HashSet();
set.add("abcdsed");
set.add("bceffgh");
set.add("eahdsf");
CheckComboBox box = new CheckComboBox(set);
CheckComboBox box1 = new CheckComboBox(set);
//frame.setLayout(new FlowLayout());
//table.getColumnModel().getColumn(0).setCellRendere r( new DefaultTableCellRenderer());
table.getColumnModel().getColumn(0).setCellEditor( new DefaultCellEditor(box));
frame.add(box1,BorderLayout.NORTH);
frame.add(table,BorderLayout.SOUTH);
frame.setSize(300,300);
frame.setVisible(true);
}
class MyTableModel extends AbstractTableModel {
private String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"};
private Object[][] data = new Object[4][5];

public int getColumnCount() {
return columnNames.length;
}

public int getRowCount() {
return data.length;
}

public String getColumnName(int col) {
return columnNames[col];
}

public Object getValueAt(int row, int col) {
return data[row][col];
}





public boolean isCellEditable(int row, int col) {


return true;

}

public void setValueAt(Object value, int row, int col) {

if(value instanceof JCheckBox)
{

}
data[row][col] = value;
//fireTableCellUpdated(row, col);


}


}

}