wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes JDateChooser in JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JDateChooser in JTable" Watch "JDateChooser in JTable" New topic
Author

JDateChooser in JTable

Jill Heaney
Greenhorn

Joined: Feb 07, 2007
Posts: 2
Hi

I have a JTable that has a JDateChooser (toedter) in one of the columns. I have created a custom editor/renderer which sort of works, however I'm seeing 2 problems :
1. I can't remove a date (i.e. set it back to null)
2. I can't edit the date by typing into the field (this isn't too much of a problem)

Is anyone who is using toedter JDateChooser got a solution ?

My editor is :

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.util.Date;
import javax.swing.AbstractCellEditor;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;
import windows.Properties;
import com.toedter.calendar.JDateChooser;


public class DateEditer extends AbstractCellEditor implements TableCellEditor {

private JDateChooser theSpinner;
protected Object value;

public DateEditer() {
theSpinner = new JDateChooser();
theSpinner.setDateFormatString( "dd MMM yyyy" );
theSpinner.setOpaque(true);
}

public Object getCellEditorValue() {
return theSpinner.getDate();
}

public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
// If the date is blank then default to today
if (value == null) {
value = new Date();
}

theSpinner.setDate((Date)value);
if (isSelected) {
theSpinner.setBackground(table.getSelectionBackground());
} else {
theSpinner.setBackground(table.getBackground());
}
return theSpinner;
}
}



Thanks v much
Jill
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Jill Heaney:
Hi

I have a JTable that has a JDateChooser (toedter) in one of the columns. I have created a custom editor/renderer which sort of works, however I'm seeing 2 problems :
1. I can't remove a date (i.e. set it back to null)
2. I can't edit the date by typing into the field (this isn't too much of a problem)


Well I've never heard of "toedter JDateChooser" so take what I say here with a grain of salt, but presuming it doesn't do anything too weird perhaps I can help. Let me take your two issues in reverse order.

2. As far as editing the field via typing, you probably just need to call the spinner's commitEdit() method before getting its value. It would look something like this:
Except you probably have to catch the ParseException that commitEdit() might throw.

1. Setting a date "back to null" is trickier because spinners aren't designed to work this way. However, you should be able to detect if the user has cleared out the field with something like this:

This won't work if com.toedter.calendar.JDateChooser doesn't use a subclass of JSpinner.DefaultEditor for it's editor component, which is possible. I'll leave it to you to check.


While I'm here, allow me to make a couple comments:

A. What does com.toedter.calendar.JDateChooser give you that you couldn't get from JSpinner alone? For example, If you changed the constructor of your cell editor to
and made the other necessary changes (change the type of the theSpinner field to JSpinner, change calls to theSpinner.get/setDate() to theSpinner.get/setValue(), etc.) do you lose anything?

B. You could consider using a cell editor that uses a JFormattedTextField instead of a JSpinner. Both classes have the built-in date handling, and do things like allowing the user to increment/decrement dates using the arrow keys. But JFormattedTextField doesn't have those little arrow buttons.


bitguru blog
Jill Heaney
Greenhorn

Joined: Feb 07, 2007
Posts: 2
Hi Brian

Thanks very much - it all worked like a treat.
Toedter provides a nice calendar display which I was using in another screen, but you were right - just used a JSpinner.
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Brian Cole:
B. You could consider using a cell editor that uses a JFormattedTextField instead of a JSpinner. Both classes have the built-in date handling, and do things like allowing the user to increment/decrement dates using the arrow keys.


For posterity, such a cell editor would like like:



Or could instead subclass DefaultCellEditor. The code is actually longer, but this way we implement the TableCellEditor interface for free:

 
wood burning stoves
 
subject: JDateChooser in JTable
 
Similar Threads
JRadioButton in JTable
JTable selected row problem
Color in JTable
jtable
problem in JTable