• 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

JDateChooser in JTable

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Jill Heaney
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:

reply
    Bookmark Topic Watch Topic
  • New Topic