aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes display a checkbox in a table Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "display a checkbox in a table" Watch "display a checkbox in a table" New topic
Author

display a checkbox in a table

mark smith
Ranch Hand

Joined: Apr 05, 2005
Posts: 250
hi

i try to display a check in a jtable



now my AttributeTableModel class



my class Attribute


i was thinking i had only to put boolean field to display the checkbox...

thanks
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2202
    
    7
You getValueAt(...) and setValueAt(...) methods make no sense.

According your the column header you have 7 pieces of information. Therefore your swicth statement needs to handle 7 different cases and return the appropriate piece of data. This is:

if column == 0, return name
if column == 1, return code
if column == 2, return dataType
etc...
mark smith
Ranch Hand

Joined: Apr 05, 2005
Posts: 250
Rob Camick wrote:You getValueAt(...) and setValueAt(...) methods make no sense.

According your the column header you have 7 pieces of information. Therefore your swicth statement needs to handle 7 different cases and return the appropriate piece of data. This is:

if column == 0, return name
if column == 1, return code
if column == 2, return dataType
etc...


ok i fixed it with



i type a value in the grid, i see my value... but when i click outside row..... i get a null pointer exception on




i put

to bypass this problem, no sure is the way to go...

i need to go out of the jtable and come back to see the checkbox...

why i don't see a checkbox in the boolean field?

any idea?
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
mark smith wrote:but when i click outside row..... i get a null pointer exception on

i put
to bypass this problem, no sure is the way to go...

Wouldn't it be easier to just do something like this?

why i don't see a checkbox in the boolean field?

any idea?


Well if it gets a null (or Object.class) instead of Boolean.class when it first calls getColumnClass() then that's what will happen. It won't realize it should use the checkbox renderer.


bitguru blog
mark smith
Ranch Hand

Joined: Apr 05, 2005
Posts: 250
Brian Cole wrote:
Wouldn't it be easier to just do something like this?


i tried that but i get


if i put

i get



i click go out of the jtable and come back... i get a java.lang.NullPointerException at the same place...
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2202
    
    7
Well, don't add String data to your TableModel.
mark smith
Ranch Hand

Joined: Apr 05, 2005
Posts: 250
Rob Camick wrote:Well, don't add String data to your TableModel.


i don't really understand why i could not add string to y my tableModel...



so on the web, the code for getColumClass is always very basic...



so i don't understand whey that don't work...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Because check boxes only work for Booleans perhaps? You can add Strings to your TableModel all you want, just not for the check box column.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
santhosh varala kumar
Greenhorn

Joined: Nov 05, 2009
Posts: 25
Rob Prime wrote:Because check boxes only work for Booleans perhaps? You can add Strings to your TableModel all you want, just not for the check box column.


1. In TableModel, getColumnClass() should return Boolean.class insteadof String.class
and the values in that column should be boolean value.

mark smith
Ranch Hand

Joined: Apr 05, 2005
Posts: 250
with that code, that should be ok


why that return a string... when i specify a Boolean

that should return the correct type for each column...

one thing i found very very strange

i have put


in getColumnClass, when the jtable is displayed, the checkbox is not displayed... but if i click on a string column.... and after a click in a boolean column, i see the check box..

but if i click directely in a boolean column... the checkbox is not displayed...

it's like the type change....

habitually we must use directely getColumnClass or we use a render to display the kind of data in the jtable?
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
mark smith wrote:
why that return a string... when i specify a Boolean

that should return the correct type for each column...


It returns whatever you tell it to return in the getValueAt(). If you say you will return a Boolean than it expects you to return a Boolean, hence the ClassCastException when you surprise it by returning a String.

If you are expecting the code to somehow convert the the non-Boolean value to a Boolean behind the scenes then you will be disappointed.
mark smith
Ranch Hand

Joined: Apr 05, 2005
Posts: 250
Brian Cole wrote:
mark smith wrote:
why that return a string... when i specify a Boolean

that should return the correct type for each column...


It returns whatever you tell it to return in the getValueAt(). If you say you will return a Boolean than it expects you to return a Boolean, hence the ClassCastException when you surprise it by returning a String.

If you are expecting the code to somehow convert the the non-Boolean value to a Boolean behind the scenes then you will be disappointed.


actually i do:



the first time the jtable is displayed, attributes is null... so i put empty... so how to put the check box to be able to enable or disabled it...
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2202
    
    7
the first time the jtable is displayed, attributes is null... so i tried to add this code...


Well, attributes should NEVER be null (it may contain 0 rows).
mark smith
Ranch Hand

Joined: Apr 05, 2005
Posts: 250
Rob Camick wrote:
the first time the jtable is displayed, attributes is null... so i tried to add this code...


Well, attributes should NEVER be null (it may contain 0 rows).


hum the first time the jtable is displayed, there are not data... so the list is null
is not correct?

i added this code

that work...
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2202
    
    7
i added this code


Well, that is hack fix. You have a fundamental problem with your table. You should design the TableModel such that the attributes ArrayList is NEVER null.
mark smith
Ranch Hand

Joined: Apr 05, 2005
Posts: 250
Rob Camick wrote:
i added this code


Well, that is hack fix. You have a fundamental problem with your table. You should design the TableModel such that the attributes ArrayList is NEVER null.


i can put empty value... but i don't find that very logic... create a empty row only to display a jtable... seem weird
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
mark smith wrote:
i can put empty value... but i don't find that very logic... create a empty row only to display a jtable... seem weird


Just because the ArrayList is non-null, doesn't mean it contains any rows. After construction, size() returns zero, right?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2202
    
    7
i can put empty value... but i don't find that very logic... create a empty row only to display a jtable...


You apparently don't understand the concept between an ArrayList being "null" versus it being "empty".



There is no need to add an "empty row" to the model.

To understand what I mean forget about your custom TableModel and do a quick test using the DefaultTableModel. Use:


to see what the difference is.




 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: display a checkbox in a table