This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Checkbox not working in JTable. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply locked New topic
Author

Checkbox not working in JTable.

Sharad
Greenhorn

Joined: Aug 04, 2008
Posts: 10
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

Joined: Oct 27, 2005
Posts: 19654
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Sharad
Greenhorn

Joined: Aug 04, 2008
Posts: 10
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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Checkbox not working in JTable.
 
Similar Threads
jtable
Checkbox editor for JTable column
Add checkbox in JTable
JTable Custon Renderes
JCheckBox in JTable