aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Null Value 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 "Null Value in JTable" Watch "Null Value in JTable" New topic
Author

Null Value in JTable

Giuseppa Cefalu
Ranch Hand

Joined: Jul 15, 2011
Posts: 121
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



Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
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
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
. . . but what will happen if every value in the row is null?
Giuseppa Cefalu
Ranch Hand

Joined: Jul 15, 2011
Posts: 121
Sorry. I mean Date.class. I will try what you suggested. Thank you
Giuseppa Cefalu
Ranch Hand

Joined: Jul 15, 2011
Posts: 121
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

Joined: Jul 15, 2011
Posts: 121
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
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
There are methods in the TableModel interface which can probably get a class type.
Giuseppa Cefalu
Ranch Hand

Joined: Jul 15, 2011
Posts: 121
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?

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2091
    
    7
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

Joined: Jul 15, 2011
Posts: 121
Wonderfull! Thank you
Giuseppa Cefalu
Ranch Hand

Joined: Jul 15, 2011
Posts: 121
Thanks a lot! The code below does the job.



Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2091
    
    7
Actually I think the outer loop should be on the column and the inner loop on the row.
Giuseppa Cefalu
Ranch Hand

Joined: Jul 15, 2011
Posts: 121
Ok thanks. I will try that.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
It was the getColumnClass() method I was thinking about earlier.
Giuseppa Cefalu
Ranch Hand

Joined: Jul 15, 2011
Posts: 121
I do not understand your post. Could you explain?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
I was hoping you would find the getColumnClass method, which you have now been shown.
Giuseppa Cefalu
Ranch Hand

Joined: Jul 15, 2011
Posts: 121
I implement the getColummClass method in a different way. Here is the code:

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2091
    
    7
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

Joined: Jul 15, 2011
Posts: 121
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.
 
subject: Null Value in JTable
 
Similar Threads
jtable
JTable date edit problem
Jtable Cell and ENTER key
JTable
JCheckBox in JTable