• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

display a checkbox in a table

 
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

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.
 
mark smith
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, don't add String data to your TableModel.
 
mark smith
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

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

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

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

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

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

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

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

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

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.




 
reply
    Bookmark Topic Watch Topic
  • New Topic