• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

JTable dates

 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a way to allow a user to edit a date in a JTable? All of the columns I have in my table are open for editing, but when I click on the date column I cannot open it for editing. Is there something special that needs to be done for dates, or are dates not editable?
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Is there something special that needs to be done for dates



No.

but when I click on the date column I cannot open it for editing



Where is your SSCCE that demonstrates this?
 
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Lipay wrote:Is there something special that needs to be done for dates, or are dates not editable?


All you need to do is make your model's getColumnClass method return Date.class for the column. But something tells me you've already done that.
 
Mike Lipay
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I didn't want to post the code if editing dates wasn't possible.

Ever other field in the table edits with no problem, and I can't see what I missed for the date.

 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I didn't want to post the code if editing dates wasn't possible.



Yes, well the problem is that people don't know how to ask meaningfull questions with all the relevant informationand we end up wasting time guessing what you might be talking about. That is why we ALWAYS need a SSCCE so we can tell exactly what you are talking about.

Your question was about "dates" for which both Rob's assumed you where talking about the Date class, not the GregorianCalendar class. There is a big difference. The default editor needs to be able to take the data entered into the the text field and create a new Object of the column class type. The Date class does have a constructor that takes a String as a parameter and therefore you can create a new Date object.

The GregorianCalendar class does NOT take a string as a parameter for creating an object, therefore the default editor doesn't work. You need to create a custom GregorianCalendar editor and override the approriate method to make sure you can create and return a GregorianCalendar object based on the date information entered in the editor. The Swing tutorial gives an example of creating a custom editor.
 
Mike Lipay
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried using the Date class first, but received a warning from the compiler about Date being deprecated. Is there a way around that other than using GregorianCalendar?
 
Sheriff
Posts: 28394
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Date class isn't deprecated, so I expect your compiler error didn't really say that. Posting the actual error message also helps, whereas posting crude paraphrases of the error message is just distracting.
 
Mike Lipay
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:The Date class isn't deprecated, so I expect your compiler error didn't really say that. Posting the actual error message also helps, whereas posting crude paraphrases of the error message is just distracting.



Here is the message:
 
Paul Clapham
Sheriff
Posts: 28394
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay. So what does that error message actually tell you about the Date class? And what does the API documentation recommend you should do instead?
 
Mike Lipay
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Okay. So what does that error message actually tell you about the Date class? And what does the API documentation recommend you should do instead?



It tells me that Date is on the way out, what I could find tells me to use Calendar instead. Tried that:



Tossed this error during compile:


According to the examples on java.sun.com what I coded was correct. Looking elsewhere for solutions is what led me to GregorianCalendar.
 
Paul Clapham
Sheriff
Posts: 28394
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay. So the API documentation tells you to use a Calendar object if you want to create a new Date like that. But using the Calendar object as part of your data model, and not just as a tool for creating a Date, is going beyond what I see the documentation saying. You will notice that the Date class itself isn't deprecated; everything which is deprecated in Date is methods which deal with components of the Date, like month or year. The rest is fine and I would still use Date objects in my data model. In fact I do.
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You where asked to post a SSCCE for a reason.

Where in your SSCCE do you get a compile warning? You do create a Date class properly and then in fact add the Date to the TableModel. So why does your getColumnClass() method say you are adding a GregorianCalendar to the model? Why do you think the editor will be able to convert a Date to a GregorianCalendar?
 
Rob Spoor
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know exactly what the problem is. Your model says it returns instances of GregorianCalendar, but the model contains instances of Date. Change the model to return Date.class and you can edit those cells. I most certainly can.

Oh, and that code is not an SSCCE. I don't have class GBC on my system so I needed to translate it into a regular GridBagConstraints object first.
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Lipay wrote:
It tells me that Date is on the way out, ...


It says nothing of the kind. Instead it states that the constructor you are using is deprecated not that Date class itself is not deprecated, and in fact, it is not. This may seem like nitpicking, but it's actually a very important distinction that you must understand.
 
Mike Lipay
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, but a lot of this just doesn't make sense. I see nowhere where I would get the idea that I have to use a mixture of Date and Calendar. From the Calendar API I would assume that this code should work:



Calendar() Constructs a Calendar with the default time zone and locale.
set(int year, int month, int date) Sets the values for the fields year, month, and date.

It doesn't.


 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I give up, since you are obviously paying no attention to what I've said in this posting or others.

Why did you last reply include code for a Calendar object? This posting is about using the Date class not the Calendar class. The SSCCE you posted demonstrated your problem and you only need to change one line to fix it. Both Rob's have told you how to fix it.
 
Mike Lipay
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Camick wrote:Well I give up, since you are obviously paying no attention to what I've said in this posting or others.

Why did you last reply include code for a Calendar object? This posting is about using the Date class not the Calendar class. The SSCCE you posted demonstrated your problem and you only need to change one line to fix it. Both Rob's have told you how to fix it.



Yes, I am paying attention, to several different conversations here. I changed GregorianCalendar to Date, after which I started receiving the deprecation error. Then I was told to see why, the API said to use Calendar because Date(int,int,int) was no longer supported. So I started explaining the problems I had using the Calendar class, i.e., the .set(int,int,int) method is returning errors, which is what sent me to GregorianCalendar to begin with.

I am paying attention to everyone here, and trying to answer and test what everyone suggests. And believe me, I am more confused than ever with trying to follow everything at the same time.
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I changed GregorianCalendar to Date,



Why? Did I not already state once before that your code was correct and the problem was with the getColumnClass() method?

In fact both Rob's stated the same problem. So I have no idea why you can't fix your original SSCCE to get it working. Its a one line change.
 
Mike Lipay
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry, mistyped, I did correct the code:




I was trying to answer questions that people were asking.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic