aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JTable date edit problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable date edit problem" Watch "JTable date edit problem" New topic
Author

JTable date edit problem

Giuseppa Cefalu
Ranch Hand

Joined: Jul 15, 2011
Posts: 121
Hi,

I am working on a JTable AbstractTableModel application. I made the table editable, and I can edit String and Integer values; however, the date column cells do not respont to a mouse click and cannot be edited. Is there any particular way to make date cells editable. Thank you for ypur help.

Giuseppa

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
Why are you creating a custom model? Just use the DefaultTableModel:

Giuseppa Cefalu
Ranch Hand

Joined: Jul 15, 2011
Posts: 121
Thank you Rob. I will try it; but could you check my new post?

Giuseppa
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19792
    
  20

Next time, keep it all in one thread please. For those interested, the code is found here.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19792
    
  20

If the cell is not editable, then rowNew must be false and the column number must be 0.
Arun Chidam
Ranch Hand

Joined: May 08, 2006
Posts: 79


SCJP 1.4, SCWCD 1.4
Giuseppa Cefalu
Ranch Hand

Joined: Jul 15, 2011
Posts: 121
Thank you all for your replies. This is the way to do it. The cell renderer is for display and formatting; and the cell editor if for editing

Giuseppa



Giuseppa Cefalu
Ranch Hand

Joined: Jul 15, 2011
Posts: 121
Does any body know how to implement the other procedures for public class CellEditor extends DefaultCellEditor? The tabble accepts insertion of the cursor and changing of the date on the cell; but in order for the change to occur in the internal data structure, I need to be able to move the cursor to the next cell using the arrow key. I think that in order to accomplish this I need to implement other methods.

Thank you,

Giuseppa

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19792
    
  20

The following are the default renderer and editor for dates, as found in the source* of JTable:
The problem with both is that neither allows you to specify the DateFormat to use. For the renderer that's easy - simply change the line that initializes formatter. The editor is a bit trickier - you need to reimplement stopCellEditing and getTableCellEditorComponent:
Here, format is a DateFormat object to use. As you can see, the code is still very similar to the GenericEditor. The differences are using a null Date if the cell content is empty, and using DateFormat for both parsing and displaying, instead of using a sole constructor that takes a String and the toString method.


* check out file src.zip inside the JDK folder.
Giuseppa Cefalu
Ranch Hand

Joined: Jul 15, 2011
Posts: 121
Hi,

Thank you Rob.

This time I have added the editor class to my TableEditor. I can insert the cursor and move the cursor from one cell to onother; ALso I can insert new text and overwrite the cell; but the contents for date in the program data structure do not get edited. Could you help me with this please?

Thank you

Giuseppa
Giuseppa Cefalu
Ranch Hand

Joined: Jul 15, 2011
Posts: 121

Hi,

Thank you Rob.

This time I have added the editor class to my TableEditor. I can insert the cursor and move the cursor from one cell to onother; ALso I can insert new text and overwrite the cell; but the contents for date in the program data structure do not get edited. Could you help me with this please?

Thank you

Giuseppa
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19792
    
  20

1) why did you install the editor for java.sql.Date, and not for java.util.Date?
2) your table and table model never specify that the specific column contains dates. Either the table or the table model should implement getColumnClass to return Date.class (or java.sql.Date.class) for that column.
Giuseppa Cefalu
Ranch Hand

Joined: Jul 15, 2011
Posts: 121
Hi Rob,

I figured out why my date cells were not editable. I am reading from a database and the date type in mysql database is java.sql.Date. The table getColumnClass(int col) method is supposed to return a Java Date, an integer, a String .....; but not a java.sql.Date Type. When an object.class is returned by the getColumnClass(int col) method the table returns a java date format for the column, and some how because the real data type is java.sql.Date the date is not editable (does not work). In order for the date cell to be editable, I need to format the Date as java Date or as a string. I prefer to format it as a string, so that way I can present the user with the desired format. Here is the code. There is no need to implement a renderer or an editor unless you want to implement special features (ie; color).

Haris Tasawar
Ranch Hand

Joined: Aug 19, 2011
Posts: 30
hey Giuseppa,
Man how did you inherited the ResultSet metadata in the class???.......... I did everything else but am stuck in this because i cant get the resultset to be available in this class, could you tell me how did you do it???
Haris Tasawar
Ranch Hand

Joined: Aug 19, 2011
Posts: 30
sorry i forgot to point out the line. It is this line in which i have a problem???

switch(md.getColumnType(column + 1))

Apparently, it cant find 'md' in this???


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19792
    
  20

I'm guessing that md is an instance of ResultSetMetaData, retrieved by calling getMetaData() on the ResultSet.
Giuseppa Cefalu
Ranch Hand

Joined: Jul 15, 2011
Posts: 121
Is this what you need?



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable date edit problem