Daan Heuvelbeuk

Ranch Hand
+ Follow
since Aug 16, 2015
Daan likes ...
Netscape MySQL Database Windows XP
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
2
Received in last 30 days
0
Total given
21
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Daan Heuvelbeuk

I created an app with a JTable in it. I changed some code, and now the colouring of the rows is off.

Some code (so you see I (try to) set the background colour):

This is what I got. First the unselected row:


Then the same row, now selected (only the last cell is editable):


In an earlier version, I had not set the background. It turned out White. It looks like:


Anyone know why it does not work?
6 years ago


And


Logging when I run the application:
E:\Workspace\Test>java ProblemChild
setValue , value='00:10:00' as read at start method
setValue '00:10:00' - java.lang.IllegalArgumentException: Cannot format given Object as a Date
setValue , value='00:33:00' as read at start method
setValue '00:33:00' - java.lang.IllegalArgumentException: Cannot format given Object as a Date
setValue , value='01:15:00' as read at start method
setValue '01:15:00' - java.lang.IllegalArgumentException: Cannot format given Object as a Date
setValue , value='00:10:00' as read at start method
setValue '00:10:00' - java.lang.IllegalArgumentException: Cannot format given Object as a Date
setValue , value='00:33:00' as read at start method
setValue '00:33:00' - java.lang.IllegalArgumentException: Cannot format given Object as a Date
setValue , value='01:15:00' as read at start method
setValue '01:15:00' - java.lang.IllegalArgumentException: Cannot format given Object as a Date


The object data I read in my actual program comes from a database. I works fine. I could enter time (worked on an activity) in the JTable of my application, which is then written to the database. I got the problem when I wanted to add functionality to record "working on an activity of a project". With a click on the row the recorded time is added to the time already on the activity, written to the database, and the table (model.setValueAt(some code)). This then generates the errors as mentioned above.
6 years ago
After some time I picked up a project I worked on. I'm an on and off type of developer, so my skills get rusty in time. Now I am stumped.

I have a JTable in which I record time worked on an activity. I wanted to offer the user the ability to say (s)he wanted to start working on an activity (by selecting a row). After some time (s)he could click on it again, and the application will calculate how much time has passed. That works like a charm. But when I write the new time to the JTable I get an java.lang.IllegalArgumentException: Cannot format given Object as a Date. The format renderer I use is based on Table Format Renderers from Rob Carmick.



And this is the logging I get:
setValue , value='2018-02-01' as read at start method
setValue , value='00:11:00' as read at start method
setValue '00:11:00' - java.lang.IllegalArgumentException: Cannot format given Object as a Date


I have been working on this problem for a few days now. I have tried numerous things, but cannot work it out. Hence my question. What am I doing wrong?

I had a discussion on this topic before: Field JTable not editable with (among others) Rob Camick.
6 years ago

Paul Clapham wrote:Why isn't the system default ZoneId what you want? If it is what you want, then instead of



instead you use



ZoneId.systemDefault(); seemed to be the solution.

My logging is now:

And my database now contains 2017-05-04 10:59:46

It was the first time I worked with ZoneId, so I did not know (find, see) the option systemDefault();

Thanks all for the help.
I tried to change my code so it would return a java.sql.Timestamp to the database. But alas, I get a NullPointerException.



This solution (line 155) is what I found searching on the Internet (Java - Convert java.time.Instant to java.sql.Timestamp without Zone offset). What would an appropriate way be to convert my Instant to java.sql.Timestamp?
And if needed, this is the script I used to create the table:

Dave Tolls wrote:You just pass the Date or Timestamp into the PreparedStatement.
In this case, into the Object[] of values you seem to be using, rather than using that getTimestampString method.



I changed the code so it now uses an Instant instead of a String. The result is supplied below the code:
@Knute: I used the DAO tutorial - the data layer as starting point of my application.



@Dave: The TkTimestamp class is the "Model class which represents a row from the table 'tktimestamp'".

And you are right about me setting Dates as Strings. I was under the impression I should do that. How does one set "DATE and TIMESTAMP columns [] using Date and Timestamp objects"?

Dave Tolls wrote:That error looks to me like you are trying to do a PreparedStatement.setString passing in an object of type nl.mycumpanie.tk.dao.TkTimestamp.
Which looks wrong.

What does that bit of code look like?

You shouldn't have to worry about formats for any of these, if your column is a DATE or TIMESTAMP column (or equivalent), and you are setting it using PreparedStatement.setDate (or setTimestamp).



As far as I see it, the java.time.Instant is ISO-8601 compliant, meaning it has the format "yyyy-MM-ddTHH:mm:sssZ". This is not compatible with the format required for the MySQL timestamp field, meaning "yyyy-MM-dd HH:mm:ss". I have to convert to and fro ;-)

And the code I'm using is:

Paul Clapham wrote:You seem to be doing string-fiddling to convert between java.sql.Timestamp and java.time.Instant. There's no need to do that: Timestamp has a static method Timestamp.from(Instant) which converts an Instant object to a Timestamp object, and an instance method toInstant() which returns an Instant object.



You are right that I've my times confused. However, when I write the timestamp from Instant to my database (field of type MySQL timestamp) I get the error:

When I change back my code it works with a charm.


Eventually I want to let Timestamp handle all my time calculations.

And to answer my original question, I should use Instant.parse(dbTimeString);

Knute Snortum wrote:Does this site help?  I found it by googling java 8 date to instant.



Thanks Knute. I found this tutorial at Oracle: Trail: Date Time: Table of Contents
I'm working with instants which I store as MySQL timestamp. When I read the timestamp from the database I want to compare it to now (Instant timestamp = Instant.now()). Instant has a compareTo(Instant otherInstant) facility, but then I need to convert the MySQL timestamp to an Instant. Is that possible?

FWIW:
  • MySQL timestamp format is '2017-05-01 11:45:02'
  • java.time.Instant timestamp format is '2017-05-01T11:45:02'


  • I removed the T from the Instant timestamp to be able to store the timestamp in the database. So it is no problem to get the T back into the string. But then from string to instant ...
    Am I correct in concluding I can use event.getValueIsAdjusting() to filter out the second (or first, dealers choice) event out of the two that are fired when you select the row?

    Example code on the page you mentioned. Search for getValueIsAdjusting
    7 years ago
    Can anyone tell me how many events you will get if you check on selecting a row in a JTable, and why? If I select a row in my JTable I get 2 events, and would like to filter one out.
    7 years ago
    Although the project object was created, it was created inside the if loop. Outside it the project object does not exist. That's why the two instances (?) of project where differently coloured, and I got the NullPointerException. So clearly a DeveloperError.