Skip to main content

Date Format in DatePickerCellEditor

8 replies [Last post]
lostinswing
Offline
Joined: 2011-02-01
Points: 0

I have a DatePickerCellEditor in a JTable. Passed in a SimpleDateFormat object to the constructor expecting that after a date was selected it would be rendered in the cell using the date format provided. However, it is not using the specified format. It looks like it's calling toString on the Date object. Any recommendations?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kschaefe
Offline
Joined: 2006-06-08
Points: 0

You need to use a cell renderer that renders the object as you want. By default, it is always toString. The SwingX way is to pass a StringValue into DefaultTableRenderer that formats the object as you see fit. See StringValue examples in StringValues.
Karl

lostinswing
Offline
Joined: 2011-02-01
Points: 0

Here is a sample app the exhibits the problem:
<code>
package test;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Window;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.SimpleDateFormat;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import org.jdesktop.swingx.table.DatePickerCellEditor;

public class Table2 extends JFrame {

private JTable table;
private JScrollPane scrollPane;
private static SimpleDateFormat sdf_brief = new SimpleDateFormat("MM/dd/yy");

public Table2() {
super("Table Demo 2");

table = new JTable(new DefaultTableModel(1, 3));
DatePickerCellEditor datePickerCellEditor = new DatePickerCellEditor(sdf_brief);
datePickerCellEditor.setClickCountToStart(1);
datePickerCellEditor.setFormats(sdf_brief);
table.getColumnModel().getColumn(2).setCellEditor(datePickerCellEditor);
table.setPreferredScrollableViewportSize(new Dimension(400, 200));

scrollPane = new JScrollPane(table);
setContentPane(scrollPane);
}

public static void main(String[] args) {
Frame frame = new Table2();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
Window window = e.getWindow();
window.setVisible(false);
window.dispose();
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
}

}
</code>
Am I misunderstanding the API or is it a bug in DatePickerCellEditor?

lostinswing
Offline
Joined: 2011-02-01
Points: 0

I have added a DefaultTableRenderer. After selecting a value from the calendar drop down, the date is still being displayed in the cell using toString. If I then click on the cell, it reformats the date using the DefaultTableRenderer/StringValue. I would like the date formated after it is selected from the calendar drop down so that the user does not have to click in the cell. How would I accomplish this? Thanks for your help.

<code>
package test;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Window;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Date;
import java.text.SimpleDateFormat;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;

import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.renderer.DefaultTableRenderer;
import org.jdesktop.swingx.renderer.FormatStringValue;
import org.jdesktop.swingx.table.DatePickerCellEditor;

public class Table2 extends JFrame {

private JXTable table;
private JScrollPane scrollPane;
private static SimpleDateFormat sdf_brief = new SimpleDateFormat("MM/dd/yy");
private static FormatStringValue stringValue = new FormatStringValue(sdf_brief);

public Table2() {
super("Table Demo 2");

table = new JXTable(new DefaultTableModel(1, 3));
DatePickerCellEditor datePickerCellEditor = new DatePickerCellEditor(sdf_brief);
datePickerCellEditor.setClickCountToStart(-1);

table.setDefaultRenderer(Date.class, new DefaultTableRenderer(
stringValue, JLabel.RIGHT));
table.setDefaultRenderer(DatePickerCellEditor.class, new DefaultTableRenderer(
stringValue, JLabel.RIGHT));

table.getColumnModel().getColumn(2).setCellEditor(datePickerCellEditor);
table.setPreferredScrollableViewportSize(new Dimension(400, 200));

scrollPane = new JScrollPane(table);
setContentPane(scrollPane);
}

public static void main(String[] args) {
Frame frame = new Table2();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
Window window = e.getWindow();
window.setVisible(false);
window.dispose();
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
}

}

</code>

kleopatra
Offline
Joined: 2003-06-11
Points: 0

table.setDefaultRenderer(Date.class, ....)
you don't have editors as cell values <g>
Cheers
Jeanette

lostinswing
Offline
Joined: 2011-02-01
Points: 0

Thank you kleopatra. I set the CellRenderer for the column containing the calendar and it worked. However, if a DateFormat is passed in on the constructor and it is not iused to render the date in the cell, what is the point of passing in the DateFormat?

Here is the code updated to use the CellRenderer just in case someone else might find it useful:

<code>
package test;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Window;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.SimpleDateFormat;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;

import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.renderer.DefaultTableRenderer;
import org.jdesktop.swingx.renderer.FormatStringValue;
import org.jdesktop.swingx.table.DatePickerCellEditor;

public class TableWithCalendarDemo extends JFrame {

private JXTable table;
private JScrollPane scrollPane;
private static SimpleDateFormat sdf_brief = new SimpleDateFormat("MM/dd/yy");
private static FormatStringValue stringValue = new FormatStringValue(sdf_brief);

public TableWithCalendarDemo() {
super("TableWithCalendarDemo");

table = new JXTable(new DefaultTableModel(1, 3));
DatePickerCellEditor datePickerCellEditor = new DatePickerCellEditor(sdf_brief);
datePickerCellEditor.setClickCountToStart(0);

table.getColumnModel().getColumn(2).setCellEditor(datePickerCellEditor);
table.getColumnModel().getColumn(2).setCellRenderer(new DefaultTableRenderer(
stringValue, JLabel.RIGHT));
table.setPreferredScrollableViewportSize(new Dimension(400, 200));
scrollPane = new JScrollPane(table);
setContentPane(scrollPane);
}

public static void main(String[] args) {
Frame frame = new TableWithCalendarDemo();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
Window window = e.getWindow();
window.setVisible(false);
window.dispose();
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
}

}

</code>

kleopatra
Offline
Joined: 2003-06-11
Points: 0

dont know what you mean - it _is_ used. Repeating myself: you have to learn and understand
a) the difference between cellEditor vs. cellRenderer
b) the options in how to configure the table with different editors/renderers
Snoracle's online swing tutorial is a good place to start. Please note: those are bascis, unrelated to SwingX. Please keep in mind that this forum is highly focused on "X" in SwingX :-)
Cheers
Jeanette

kleopatra
Offline
Joined: 2003-06-11
Points: 0

you are misunderstanding the roles of an editor vs. a renderer :-)
Unrelated: I would recommend to not hard-code the table's prefScrollableSize in terms of pixels. Instead, use setVisibleRow/ColumnCount in JXTable. Ahhh .. just see, you use a plain JTable. Then consider to switch, otherwise you'll get only part of SwingX rendering beauty :-)
Cheers
Jeanette

lostinswing
Offline
Joined: 2011-02-01
Points: 0

Thank you Karl. I will give it a try.