• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Continously updated date field in GridBagLayout does not work

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
javax.swing.Timer.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic