File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Add checkbox  in JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Add checkbox  in JTable" Watch "Add checkbox  in JTable" New topic
Author

Add checkbox in JTable

Sharad
Greenhorn

Joined: Aug 04, 2008
Posts: 10
I am working on Swing application and my requirement is add new column that dispay the checkboxes, so user have the access to check/uncheck row for further process.
In application 3 tabbedpanes are there & in the 3rd tabbedpane table data is displaying.


//Code snippet


I tried following ways-
1)

here output is dispaying complete details of Checkbox


2)
till this point its working fine... but in above code method public Object getValueAt(int rowIndex, int columnIndex)
I am checking if(Column==0)
return Class.forName("java.lang.Boolean");

here it is throwing ClassCastException.. java.lang.Class cannot be cast to java.lang.Boolean

Please provide me solution aas soon as possible...coz from past 2 days i m stucking on this and due to this not able to move ahead on my application development.

Thanks in Advance
Sharad

[RP] added code tags [/RP]
[ October 08, 2008: Message edited by: Rob Prime ]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

Please Use Code Tags in the future. I've edited your post to include them, so at least I can read it. Also, Ease Up.


You are nearly there with option 2. Returning the Class object for Boolean should solve it. However, Boolean.class is much shorter than Class.forName("java.lang.Boolean"), and also will resolve the Class object at compile time. Plus there is no pesky ClassNotFoundException.

The problem is that getValueAt should return a Boolean object (or null) - not a Class object. Also, this value should be stored somehow - otherwise the checked state will be forgotten.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

Strong disclaimer:
I haven't tried out your code. It is 11:37 PM and its been a very hard day.

Why are you using the abstract table model? Any particular reason why you cannot use the DefaultTableModel?

Swing has built in renderers for specific types. Boolean/boolean is rendered as a JCheckBox by default.
If you subclass DefaultTableModel, all you need to do is override getColumnClass (it returns Object.class by default)
So you do something like


Best of luck.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Maneesh Godbole:
Why are you using the abstract table model? Any particular reason why you cannot use the DefaultTableModel?


This is somewhat off-topic from the original post, but I feel compelled to comment.

I strongly recommend extending AbstractTableModel over DefaultTableModel, unless there's a reason you want to inherit the Vector-of-Vector behavior.

In particular, if you plan to provide an implementation of the getValueAt() method then you probably don't want to extend DefaultTableModel.


bitguru blog
Sharad
Greenhorn

Joined: Aug 04, 2008
Posts: 10
Thanks a lot for all your suggesstions.
But its not working...


I tried below mentioned way to display checkbox and displayed it successfully. but to make it visible i need to click on that particular cell.

//code snippet
EachRowEditor1 rowEditor1 = new EachRowEditor1();
table.getColumn("Select").setCellEditor(rowEditor1);

class EachRowEditor1 implements TableCellEditor
{
protected TableCellEditor defaultEditor;
public EachRowEditor1()
{
defaultEditor = new DefaultCellEditor(new JCheckBox());
}
}

After trying this code,I am facing tese issues..

1) I need to click on every cell then only checkbox is visible.
2) Once i click another cell then checkbox of that particular cell is only visible and previouly clicked cell is again displaying nothing(just like blank TextField.


Your valuable sugesstions are highly needed.

Thanks in advance
Sharad
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Sharad Golu:

After trying this code,I am facing tese issues..

1) I need to click on every cell then only checkbox is visible.
2) Once i click another cell then checkbox of that particular cell is only visible and previouly clicked cell is again displaying nothing(just like blank TextField.


You have set up cell editors, but not cell renderers. It is the
renderers that paint all the cells except the one being edited.

That said, you shouldn't have to mess with renderers or editors
at all to get this to work. The easy way is to simply have the
getColumnClass() method return Boolean.class and have getValueAt()
return either Boolean.TRUE or Boolean.FALSE for the column in
question. It's that easy. The default renderer/editors do the rest.
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...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

Why did you change your name? Please change it back.

Originally posted by Sharad:
Rob
Last thread was reagrding adding checkbox and this is for checkbox is not working.

But it's the checkbox you just added that isn't working. So in fact, the adding itself is not successful yet. It will be once it is used correctly, both for displaying and setting.


As for the state: right after clicking on it, it calls setValueAt to set the value, then getValueAt again to display it. And that still says false. So you can't solve this without setValueAt.
[ October 11, 2008: Message edited by: Rob Prime ]
Sharad
Greenhorn

Joined: Aug 04, 2008
Posts: 10
Thanks for all your suggesstions Rob..
Its working now.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

"Sharad".

Could you please adjust your screen name to confirm to http://www.javaranch.com/name.jsp.

If I remember correctly, it was "Sharad Golu". Perhaps you dropped the second part by mistake?
Sharad
Greenhorn

Joined: Aug 04, 2008
Posts: 10
Maneesh,

long back when i created my user id on this website.. i just entered it as Sharad Golu....

Now i have make the correction and it will display only sharad further.
I haven't dropped the 2nd part by mistake
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Originally posted by Sharad:
long back when i created my user id on this website.. i just entered it as Sharad Golu....

Now i have make the correction and it will display only sharad further.
I haven't dropped the 2nd part by mistake


Please be clear:
You need to edit your profile and provide a first name and last name. If you don't, then your account will be closed. If you are unable to fix your display name, please make a request in the JavaRanch forum for an administrator to assist you.

thanks,
Dave
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
Back to the naming policy; you obviously know what it says, and you obviously know how to correct your displayed name. When your account is suspended, it won't be by mistake either.

Oh, I see DO'M has warned you too.
[ October 13, 2008: Message edited by: Campbell Ritchie ]
Sharad
Greenhorn

Joined: Aug 04, 2008
Posts: 10
ok ok....
My first & last name are already there...I have only modified the Display Name
I think "Add checkbox in Jtable" subject moved to "Add first & last Name" :roll:


Now back to thread...
I am displaying 10 rows(data is setted in model) in my Jtable with checkboxes & on clicking delete button, 5 selected rows are deleted from model(& i.e model is updated successfully with the remaining 5 records only.

But, still my table is displaying all the 10 records. All the time i need to restart my application to get the affected result..

My requirement is, if data model is upadated the JTable should also gets updated automatically with the new records only and user didnt need tio restart the application again.


Please le me know how to implemment this.

Thanks
Sharad
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

Originally posted by Sharad:
ok ok....
My first & last name are already there...I have only modified the Display Name
I think "Add checkbox in Jtable" subject moved to "Add first & last Name" :roll:

It's a policy we insist you follow. You seemed to be fine with that in the past. I suggest again that you change it back; David has already mentioned what will happen if you don't. That's not an idle threat.


Now back to thread...
I am displaying 10 rows(data is setted in model) in my Jtable with checkboxes & on clicking delete button, 5 selected rows are deleted from model(& i.e model is updated successfully with the remaining 5 records only.

But, still my table is displaying all the 10 records. All the time i need to restart my application to get the affected result..

My requirement is, if data model is upadated the JTable should also gets updated automatically with the new records only and user didnt need tio restart the application again.


Please le me know how to implemment this.

Thanks
Sharad

AbstractTableModel has some methods to fire updates. Not surprisingly, they all start with "fireTable". Check those out. In this case, you need fireTableRowsDeleted.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

"Sharad",
Welcome to the JavaRanch.

We're a friendly group, but we do require members to have valid display names.

Display names must be two words: your first name, a space, then your last name. Fictitious names are not allowed.

Please edit your profile and correct your display name since accounts with invalid display names get deleted.

Sorry to be a hard-arse, but this will be your final warning. For the record I seriously considered locking this thread until the change is made, but then I would need to come back and reopen it.

/Dave
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Sharad:
My first & last name are already there...I have only modified the Display Name


Sharad, I'm not an administrator here, and I don't really care what you set your display name to.

However I will point out that the people who run this place have an explicit naming policy (which perhaps you haven't read yet because the link above is semi-broken). You don't have to follow naming policy, of course, but if you really want help from this forum then it is in your best interests to do so. If you don't follow it they can (and will) suspend your account and freeze your threads.

All you have to do to comply is modify your display name to include a second name. It doesn't even have to be your real last name, so for instance you could be "Sharad Smith", "Sharad Jones", or Sharad anything so long as it's not obviously fictitious. (This is my reading of the naming policiy. Again, I am not an administrator, just a contributor.)

[edit: typo "lset" -> "set", also "add modify" -> "modify"]
[ October 13, 2008: Message edited by: Brian Cole ]
Sharad Kharya
Ranch Hand

Joined: Oct 15, 2008
Posts: 65
Rob,

AbstractTableModel has some methods to fire updates. Not surprisingly, they all start with "fireTable". Check those out. In this case, you need fireTableRowsDeleted.



here the problem is.. Database rows are deleted successfully but table model ramains as it is..

Say for example ---

My Jtable is displaying list of records with checkboxes...

out of 3 checkboxes in a table i select 2 checkbox and click dalete button.

selected two rows are deleted successfully from Database but table/tablemodel is remains unaffected(i.e rows deleted from DB but table model is not updating)..and displaying the same result..

I need to display only one row now with resarting my application.


Please let me know how to implement this..I am not using any kind of listeners on table as of now.

Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
Welcome back to JavaRanch
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Sharad Goluone:
here the problem is.. Database rows are deleted successfully but table model ramains as it is..

Say for example ---

My Jtable is displaying list of records with checkboxes...

out of 3 checkboxes in a table i select 2 checkbox and click dalete button.

selected two rows are deleted successfully from Database but table/tablemodel is remains unaffected(i.e rows deleted from DB but table model is not updating)..and displaying the same result..

I need to display only one row now with resarting my application.


Please let me know how to implement this..I am not using any kind of listeners on table as of now.


Did you call the fireTableRowsDeleted() method as Mr. Prime suggested?

It seems like you ignored his advice, which was not wise because forgetting to call fireTableRowsDeleted() will result in exactly the symptoms you describe.
Sharad Kharya
Ranch Hand

Joined: Oct 15, 2008
Posts: 65
Thanks all for your help guys..

This has been succssfully implemented in this way :-

Add new method in TableModel calss:


and on the listener of button i have updated model.setProductDetails with new data and calls refreshTableData().

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Add checkbox in JTable
 
Similar Threads
JTable
Checkbox editor for JTable column
Checkbox not working in JTable.
JCheckBox in JTable
JTable and its size