• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Null Value in JTable

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a JTable that reads data from a database table. Some times the information in the table is null. I have created a loop that counts, traverses the columns and asks for the data type of the value inside columns Once it determines the values' datatype. it attached the corresponding csutom editor. For exmpla if the data type is date it attaches the date editor that formats and validates dates. The problem is that if the value in a cell the first row is null; the program cannot retrieve the data type and the program braeks. Is this any way to deal with this?

Here is the code



 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's String.Date? I am not familiar with it. Have you written your own String class?

Does that code throw a NullPointerException? Can you put a test in for nullity?
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . but what will happen if every value in the row is null?
 
Giuseppa Cefalu
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry. I mean Date.class. I will try what you suggested. Thank you
 
Giuseppa Cefalu
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. What you describe works in the sense that the program does not break. In fact I have tried as the code below shows. The problem with this is that if the cell value is null, the editor does not get installed.

 
Giuseppa Cefalu
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem that I see is that if the value is equal to null there is not way to test for the data type of the column. Or may be there is a way to interrogate the data type of the column without having to retrive the value in the cell?
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are methods in the TableModel interface which can probably get a class type.
 
Giuseppa Cefalu
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the code below, I set the java.sql.Date value in the table to String. The reason for this is that if I set it to java.lung.Date, my editor does not work. The editor returns a String value because I uses SimpleDateFormat to make sure that the date is always entered as "yyyy-MM-dd" and at the same time it makes sure that month and day are not out of range. Therefore; I have to insert a String value


This is the editor



I gan get the Column Class with the method below; but It will return a String; not a Date; in which case I would not need the table.getColumnClass() method any way; if the value is null I can invoke the editor. But how do I know that it is a date?

 
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 problem is that if the value in a cell the first row is null;



Instead of just checking the first row you nee to loop through all the rows until you find a non-null object and then use that Object to determine which editor to use. If all the rows are null then just use the default editor.

I usually do this by overriding the getColumnClass(...) method:

 
Giuseppa Cefalu
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wonderfull! Thank you
 
Giuseppa Cefalu
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot! The code below does the job.



 
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
Actually I think the outer loop should be on the column and the inner loop on the row.
 
Giuseppa Cefalu
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok thanks. I will try that.
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It was the getColumnClass() method I was thinking about earlier.
 
Giuseppa Cefalu
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not understand your post. Could you explain?
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was hoping you would find the getColumnClass method, which you have now been shown.
 
Giuseppa Cefalu
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I implement the getColummClass method in a different way. Here is the code:

 
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
That is a perfectly fine implementation.

Although I might use Date.class instead of String.class. Then you have more control over how the date is displayed if you ever want to provide a custom renderer.
 
Giuseppa Cefalu
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is a good point; but the reason why I decided to return a String instead of a Date is because a java.sql.Date makes the cell non- editable even if I implement the setCelleditbale method to true. On the other hand, If I used java.util.Date, the date being displayed is formatted Month nn, nn instead of as "yyyy-MM-dd'. Then, since my editor formats the date as "yyyy-MM-dd" and returns a String instead of a Date value two things happen: first, when you click on the cell, the date formated by the editor appears on the background while the java.uitl.Date goes away, Second, the editor blocks. What happens is that since the editor returns a string (SimpleDateFromat), I have to return a String in the getColumnClass() method.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic