• Post Reply Bookmark Topic Watch Topic
  • New Topic

Defaulttable and inner table

 
Daan Heuvelbeuk
Ranch Hand
Posts: 67
MySQL Database Netscape Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my application I have a JFrame which I deploy several JPanels. One of these panels contains data from a database. When creating the table I use an inner class. I always struggle with understanding inner classes and would like to create an other class containing the necessary code. But I don't see how. Can you help?



In the constructor of MyRecordsPanel I connect to the database. In createRecordsPanel I create the actual panel displayed in the frame


I'm not fond of inner classes. It obscures what you do. I prefer to work out the code in an understandable way so when I come back in a year I do not have problems understanding what I did.

So my question is, what happens hear, and how can I get it to more understandable code?


(I know a DefaultTableModel is created using a 2 dimensional vector containing table 'data', and table headers (columnNames). On creation of the DefaultTableModel the fields of the table are read from the 2 dimensional table 'data'. And as you do not know what is in the 2D Vector of Objects you ask it to return the runtime class of the object. That is so far as I understand this code)
 
Daan Heuvelbeuk
Ranch Hand
Posts: 67
MySQL Database Netscape Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry. Found the answer, sort off. I moved the inner class code to a different class:


This makes (a bit more) sense (to me). Only the method

is strange for me.
 
Campbell Ritchie
Marshal
Posts: 52581
119
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That should probably read
public Class<?> getColumnClass(...)
…because the Class class is parameterised, but that is not important.

What you have is a different type in each column. Maybe column 3 contains a String and column 4 a long integer. Then, passing 3 would get you a reference to the Class<String> class, i.e  the Class object used to create instances of Strings. And 4 would get you the Clas object for Longs (Class<Long>).
 
Daan Heuvelbeuk
Ranch Hand
Posts: 67
MySQL Database Netscape Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the answer.

Is it not odd there is only a method getColumnClass? In the method you iterate over each row for a given column. Would it not be more logical to iterate over each column for a given row, so you follow the logic of a record?

What benefit would I get when I know what type there is in the column?

If there is a benefit I could change the code to:


Edit: That does not work. Pity. Eclipse mutters "Type mismatch: cannot convert from String (Date, Time) to Class".
 
Darryl Burke
Bartender
Posts: 5155
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daan Heuvelbeuk wrote:... when I know what type there is in the column

When the type is known, you should do something likeNote that you can't have a break; after a return statement as that would be unreachable code.
 
Paul Clapham
Sheriff
Posts: 21889
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daan Heuvelbeuk wrote:What benefit would I get when I know what type there is in the column?


The code which displays the cells will then display them in a suitable way for that type, instead of just using the toString() method of the cell's object. This is particularly helpful if the cells are editable, because then the code which edits your input will ensure that only suitable data is entered. For example it will only allow you to enter digits for an Integer column.
 
Daan Heuvelbeuk
Ranch Hand
Posts: 67
MySQL Database Netscape Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:The code which displays the cells will then display them in a suitable way for that type, instead of just using the toString() method of the cell's object. This is particularly helpful if the cells are editable, because then the code which edits your input will ensure that only suitable data is entered. For example it will only allow you to enter digits for an Integer column.


Thanks Paul. But I still have the problem that what I tried resulted in Eclipse complaining about "Type mismatch: cannot convert from String to Class<?>", or "Type mismatch: cannot convert from Date to Class<?>", or ""Type mismatch: cannot convert from Time to Class<?>". So in the end I had to go back to the old situation.

 
Paul Clapham
Sheriff
Posts: 21889
36
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daan Heuvelbeuk wrote:Thanks Paul. But I still have the problem that what I tried resulted in Eclipse complaining about "Type mismatch: cannot convert from String to Class<?>", or "Type mismatch: cannot convert from Date to Class<?>", or ""Type mismatch: cannot convert from Time to Class<?>". So in the end I had to go back to the old situation.


Even after Darryl posted the correct way to return a Class object?
 
Daan Heuvelbeuk
Ranch Hand
Posts: 67
MySQL Database Netscape Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Even after Darryl posted the correct way to return a Class object?


"Smacks face". You are right. I should have tried it. Instead I interpreted it, which I did it wrong.

Now I only have to find a way to display my time ("01:15:00"), which is stored as sql time in the database, and stored in the row with

to be displayed in the JTable as
01:15:00
and not as
1-jan-1970

I earlier solved this problem with

But off course that resulted in a complaint from the getColumnClass method (java.lang.IllegalArgumentException: Cannot format given Object as a Date). So, any suggestions?

Edit:
And when the time field is represented as date ('1-jan-1970') the field is not editable, and when it is represented as String ('01:15:00') I can change the contents.

Edit 2:
I have a sort of solution.

As you can see, I'm still struggling with the last field, the Time spend on working on an activity. The code to add the time to the JTable is:


Now the time is displayed as "1:15:00", but I cannot edit that field, and all other fields of the record are editable, while they should not be editable.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!