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?
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.
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?
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?
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:
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.