Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Continously updated date field in GridBagLayout does not work  RSS feed

 
Knut Nilson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, sorry, again one of he new people. I would like to create an automatically updated date field (second should run) but to start with I would be happy to get the date at all on the GridBagLayout.


I currently don't get my head around the diffent format issues. The problem lies in the last line, since the add does not allows the required Component, Object Constrain.
Thanks for any hint.
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try putting the String into a JLabel?
 
Knut Nilson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the hint, but unfortunately that produced another error. The current code is now:



The errror message for line 3: java.lang.IllegalArgumentException: Cannot format given Object as a Date
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knut Nilson wrote:

The errror message for line 3: java.lang.IllegalArgumentException: Cannot format given Object as a Date

You are trying to format a StringBuilder as if it were a Date. I don't think the StringBuilder is necessary at all:
 
Knut Nilson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great that worked just fine. Thanks for the quick replay. Do you also have a hint for the running clock/seonds?
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
javax.swing.Timer.
 
Campbell Ritchie
Sheriff
Posts: 53742
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knut Nilson wrote: . . . new people. . . .
Welcome to the Ranch
 
Knut Nilson
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks this was a perfect hint, I finally did it now this way.
public class Clock extends JTextField {
javax.swing.Timer m_t;

 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would probably have gone for JLabel instead of JTextField, but there's little difference.

You do have an issue with your text, a mistake many users make when working with Calendar: the month returned by get(Calendar.MONTH) starts at 0, so for today "mo" would be 3, not 4. I suggest using a DateFormat instead:
Added advantages:
1) you don't have to worry about adding extra zeros; your code could have shown a time of "4:5:3", my code will turn that into "4:05:03" because of the "mm" and "ss". You can do the same for the month and year (yyyy is quite common).
2) you don't need to create a Calendar instance; the commented out line of code would be enough. Well, unless you need timezone specifics, but usually you don't need to worry about this.

Note that the DateFormat is accessed only in the EDT and is therefore thread-safe.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!