Skip to main content

Some questions about DatePickerCellEditor

1 reply [Last post]
paultaylor
Offline
Joined: 2003-12-04
Points: 0

Hi just trying out DatePickerCellEditor (swing1.0) for adding a date in a table,

1. I would like to more easily be able to select year because the majority of times this is what my users will want to do, I read http://forums.java.net/jive/thread.jspa?threadID=60595 and found your JDNC Calendar Demo with Zoom which Id like to use but I cant see how I use it for my Date Picker.

2. On Windows 7 the Date Picker dropdown icon doesnt seem to be correct, its different to the arrow displayed by a combo box editor on another table.

3. I also have a bit of a out there requirement, the field I'm editing allows you to store any of the following
yyyy
yyyy-MM
yyyy-MM-dd

because sometimes the user only knows the year, so I want them to just be able to store the year (i.e 1980 rather than 1980-01-01) I know that this isnt really a date but I think this would be useful functionality. At the moment Ive just hacked it so that if you select date 1980-01-01 assumes you just mean 1980 and same for 1980-01, obviously no good in production.

What would be nice would be another field on the JXMonthView when it comes up letting
you select the granulity of the date you are following.

4. I dont understand why JXDatePIcker support multiple dateformats, how that is used, and also there seems a mismatch with DatePickerCellEditor that only lets you specify one format, so by default existing data has to be in that format for the DatePicker to be inilitilised with correct data

FYI:I wrap a class round DatePickerCellEditor because my table fields are Cells

{code}
public class DateCellEditor implements TableCellEditor
{
private DatePickerCellEditor datePicker;
private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
private DateFormat monthFormat = new SimpleDateFormat("yyyy-MM");
private DateFormat yearFormat = new SimpleDateFormat("yyyy");
private Cell value;
public DateCellEditor()
{
datePicker = new DatePickerCellEditor(yearFormat);
datePicker.setClickCountToStart(1);
datePicker.setFormats(dateFormat,monthFormat,yearFormat);
}

public Component getTableCellEditorComponent(JTable table, Object val,
boolean isSelected,
int row, int column)
{
value = (Cell)((Cell) val).clone();
return datePicker.getTableCellEditorComponent(table, value.getValue(), isSelected, row, column);
}

/**
* Want it to return as Cell
*/
public final Object getCellEditorValue()
{
Date date = datePicker.getCellEditorValue();
Cell cell;
if(date!=null)
{
if(date.getDate()==1 && date.getMonth()==0)
{
cell = new Cell(yearFormat.format(date));
}
else if(date.getDate()==1)
{
cell = new Cell(monthFormat.format(date));
}
else
{
cell = new Cell(dateFormat.format(date));
}
}
else
{
cell = new Cell("");
}
return cell;
}

public boolean stopCellEditing()
{
return datePicker.stopCellEditing();
}

/**
* Tells the editor to cancel editing and not accept any partially
* edited value.
*/
public void cancelCellEditing()
{
datePicker.cancelCellEditing();
}

/**
* Adds a listener to the list that's notified when the editor
* stops, or cancels editing.
*
* @param l the CellEditorListener
*/
public void addCellEditorListener(CellEditorListener l)
{
datePicker.addCellEditorListener(l);
}

/**
* Removes a listener from the list that's notified
*
* @param l the CellEditorListener
*/
public void removeCellEditorListener(CellEditorListener l)
{
datePicker.removeCellEditorListener(l);
}

public boolean shouldSelectCell(EventObject anEvent)
{
return datePicker.shouldSelectCell(anEvent);
}

public boolean isCellEditable(EventObject anEvent)
{
return datePicker.isCellEditable(anEvent);
}
}

{code}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
paultaylor
Offline
Joined: 2003-12-04
Points: 0

Try again
[code]
public class DateCellEditor implements TableCellEditor
{
private DatePickerCellEditor datePicker;
private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
private DateFormat monthFormat = new SimpleDateFormat("yyyy-MM");
private DateFormat yearFormat = new SimpleDateFormat("yyyy");
private Cell value;
public DateCellEditor()
{
datePicker = new DatePickerCellEditor(yearFormat);
datePicker.setClickCountToStart(1);
datePicker.setFormats(dateFormat,monthFormat,yearFormat);
}

public Component getTableCellEditorComponent(JTable table, Object val,
boolean isSelected,
int row, int column)
{
value = (Cell)((Cell) val).clone();
return datePicker.getTableCellEditorComponent(table, value.getValue(), isSelected, row, column);
}

/**
* Want it to return as Cell
*/
public final Object getCellEditorValue()
{
Date date = datePicker.getCellEditorValue();
Cell cell;
if(date!=null)
{
if(date.getDate()==1 && date.getMonth()==0)
{
cell = new Cell(yearFormat.format(date));
}
else if(date.getDate()==1)
{
cell = new Cell(monthFormat.format(date));
}
else
{
cell = new Cell(dateFormat.format(date));
}
}
else
{
cell = new Cell("");
}
return cell;
}

public boolean stopCellEditing()
{
return datePicker.stopCellEditing();
}

/**
* Tells the editor to cancel editing and not accept any partially
* edited value.
*/
public void cancelCellEditing()
{
datePicker.cancelCellEditing();
}

/**
* Adds a listener to the list that's notified when the editor
* stops, or cancels editing.
*
* @param l the CellEditorListener
*/
public void addCellEditorListener(CellEditorListener l)
{
datePicker.addCellEditorListener(l);
}

/**
* Removes a listener from the list that's notified
*
* @param l the CellEditorListener
*/
public void removeCellEditorListener(CellEditorListener l)
{
datePicker.removeCellEditorListener(l);
}

public boolean shouldSelectCell(EventObject anEvent)
{
return datePicker.shouldSelectCell(anEvent);
}

public boolean isCellEditable(EventObject anEvent)
{
return datePicker.isCellEditable(anEvent);
}
}

[/code]