• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Add checkbox in JTable

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sharad
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • 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...
 
Rob Spoor
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all your suggesstions Rob..
Its working now.
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Marshal
Posts: 80281
432
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 80281
432
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome back to JavaRanch
 
Brian Cole
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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().

 
Well behaved women rarely make history - Eleanor Roosevelt. tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic