This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JTable dates Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable dates" Watch "JTable dates" New topic
Author

JTable dates

Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
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?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2152
    
    7
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?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
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
Ranch Hand

Joined: Jun 13, 2009
Posts: 2152
    
    7
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

Joined: Sep 11, 2007
Posts: 171
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?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Sep 11, 2007
Posts: 171
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Sep 11, 2007
Posts: 171
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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
Ranch Hand

Joined: Jun 13, 2009
Posts: 2152
    
    7
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

Joined: Oct 27, 2005
Posts: 19649
    
  18

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.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Sep 11, 2007
Posts: 171
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
Ranch Hand

Joined: Jun 13, 2009
Posts: 2152
    
    7
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

Joined: Sep 11, 2007
Posts: 171
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
Ranch Hand

Joined: Jun 13, 2009
Posts: 2152
    
    7
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

Joined: Sep 11, 2007
Posts: 171
I'm sorry, mistyped, I did correct the code:




I was trying to answer questions that people were asking.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable dates
 
Similar Threads
JTable
JTable- getValueAt()
JDateChooser in JTable
JTable problem
JTable Problem