Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
    Bookmark Topic Watch Topic
  • New Topic

Checkbox not working in JTable.

 
Sharad
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Report post to moderator
I am working on swing application(displaying checkbox in Jtable) and facing this problem..

As an ouput MiscPanel is displaying a list of data in table format.
Column 0 = displaying checkbox
Column 1 = displaying types(string)

1) Onclicking checkboxes, ideally checkbox should selected. But nothing is happening.
2) On selecting on the types column cell, if I update the value and click on another cell then the
value on previously modified cell is unchanged and still displaying the default value in cell.

Did i need to any kind of listeners to table/tablemodel?
any other interface needs to be implemented?

Please let me know in detail how to resolve this.


//Code snippet

public JPanel getMiscPanel()
{
private String [] pNames = {"select","type"};
myMmodel= new MYTablemodel();
table = new JTable(myMmodel);
}

protected class MYTablemodel extends AbstractTableModel
{
Vector productDetails ;
MYTablemodel(){
productDetails = model.getProductDetails(); //model is storing all the data.
}
public Object getValueAt(int rowIndex, int columnIndex)
{
if (productDetails.size()>0)
{
Hashtable ht = (Hashtable) productDetails.get(rowIndex);
String str = "" ;
if(ht.containsKey(pNames[columnIndex]) && columnIndex!=0)
{
str = (String) ht.get(pNames[columnIndex]);
return str;
}else if(columnIndex==0){
return (Boolean)ht.get(pNames[columnIndex]);
}
}
return null;
}
public boolean isCellEditable(int row, int col)
{
return true;
}
public Class getColumnClass(int column)
{
if (column == 0) {
return Boolean.class;
}else{
return Object.class;
}
}
}

Thanks
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
Let's keep this all in your original thread, shall we? Closing this one.

The problem is, you need to override setValueAt as well, and set the value for the boolean column. I warned you about this in the original thread already:
Originally posted by Rob Prime:
Also, this value should be stored somehow - otherwise the checked state will be forgotten.


And another line of that post is appropriate here:
Originally posted by Rob Prime:
Please Use Code Tags in the future.
 
Sharad
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Report post to moderator
Rob
Last thread was reagrding adding checkbox and this is for checkbox is not working.

I create an another thread with proper subject line..hope this is ok..

well your suggesstion for setValueAt() will come later..because after clicking on the checkbox it is not showing even the selected(right tick) icon.

even though i didnt click anywhere else..checkbox state is as it is...
 
    Bookmark Topic Watch Topic
  • New Topic