4

I'm using a JTable. There I have a Date column, where I need to make a JXDatePicker appear when I click on a cell so that I can select a date from it.

Can someone show me how to do this?

Thanks! waiting for an answer..

Anubis
  • 6,995
  • 14
  • 56
  • 87

4 Answers4

10

You should probably use DatePickerCellEditor, which is a CellEditor using a JXDatePicker as editor component. For example:

TableColumn dateColumn = table.getColumnModel().getColumn(columnIndex);
dateColumn.setCellEditor(new DatePickerCellEditor());

Here is a demo table:

import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.TableColumn;

import org.jdesktop.swingx.table.DatePickerCellEditor;

public class DateColumnDemo {

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("DateColumnDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JTable table = new JTable(new Object[][] { { "1", new Date() } },
                new Object[] { "Id", "Time" });

        TableColumn dateColumn = table.getColumnModel().getColumn(1);
        dateColumn.setCellEditor(new DatePickerCellEditor());

        JScrollPane scrollPane = new JScrollPane(table); 

        frame.add(scrollPane);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
tenorsax
  • 21,123
  • 9
  • 60
  • 107
  • 1
    Yeah, i got that thanx it works! (Though, now i'm left with a couple of new questions.) – Anubis Aug 06 '12 at 05:17
  • 2
    For this kinda use which table do you suggest, JTable or JXTable? I'm hoping to add some user friendly functionality. And my table got about 25 columns. No limitation for number of rows...greatly appreciate any advice.. @Max – Anubis Aug 06 '12 at 05:25
  • 4
    @Anubis, depending on what functionality you want to add, I'd probably suggest the JXTable, as it has a really nice highlighting API, among other things, IMHO – MadProgrammer Aug 06 '12 at 05:29
  • 3
    @MadProgrammer, I'm following Max 's answer. I got a small issue there. picker works fine. But can't figure out how to change its format. I need '"dd-MMM-yyyy"' format but it shows something like this 'Wed Aug 01 00:00:00 IST 2012'. I've changed DatePickerCellEditors format, yeah it does something, It shows according to the set format only when I've clicked on the cell. After cell loose the focus, the cell shows that bulky value. Any help.. – Anubis Aug 06 '12 at 05:46
  • @Anubis you need to supply a CellRenderer capable of formatting the `Date` value the way you want it. Take a look at "Using Custom Renderers" http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#renderer. You use the same basic approach to `editors` to apply the `renderers` (just use the `renderer` methods instead) – MadProgrammer Aug 06 '12 at 06:40
  • @Anubis concerning the format of rendering: JXTable uses a reasonable (locale dependent) default format for rendering. If it doesn't your tableModel isn't returning Date class for the column .. – kleopatra Aug 06 '12 at 08:29
  • I ran this sample code (thanks, @tenorsax), and it worked as expected. But if I resize the window, thus changing column widths, then the date picker becomes broken. Selecting a date using the UI leaves the date the same. Has anyone seen this behavour? – Rob Scala May 23 '16 at 22:23
  • It turns out that when the table column is wider than the date-picker window, it doesn't work. It works with a narrower column. – Rob Scala May 24 '16 at 00:07
  • I solved this by calling setLightWeightPopupEnabled(false) on the JXDatePicker. – Rob Scala May 24 '16 at 00:48
5

As already mentioned in my comment to Max' correct answer:

JXTable (same as a plain JTable) does format a date value by default, using the format as returned by DateFormat.getInstance(). If the formatting appears to not work, that's typically an incomplete implementation of the tableModel: default renderer for a particular type is used only when the columnClass returns that particular type

// in your TableModel, implement getColumnClass
@Override
public Class<?> getColumnClass(int columnIndex) {
    if (columnIndex == myDateColumnIndex) {
        return Date.class;
    }
    ...
}

To install a date renderer with a custom format, instantiate a DefaultTableRenderer with a FormatStringValue as appropriate and tell the table to use it (either per-column, works with whatever columnClass or per-table, works for columns returning the Date class)

StringValue sv = new FormatStringValue(new SimpleDateForma("dd-MMMM-yyyy"));
TableCellRenderer r = new DefaultTableRenderer(sv);
// either per-column
table.getColumn(dateColumnIndex).setCellRenderer(r);
// or per-table
table.setDefaultRenderer(Date.class, r);
kleopatra
  • 51,061
  • 28
  • 99
  • 211
  • 1
    Thank you very much @kleopatra for your detailed explanation. Never knew the use of that `public Class> getColumnClass(int columnIndex){...}` method. Your answer became very useful.. – Anubis Aug 07 '12 at 03:25
3

You could supply a default table cell editor to the Date class (assuming the column is using Date)

Check out setDefaultEditor for details.

This tends to be a little heavy handed, so you could use JTable.getColumnModel().getColumn(int).setCellEditor(editor) to specify the editor to be use for a given TableColumn

Checkout How to use Tables (Using other Editors) for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

You can add a DatePicker to a JTable (or to a JXTable) by adding a "Table Editor" to your table. A table editor is any class that implements the "javax.swing.table.TableCellEditor" interface. You can do this with the JXDatePicker component as described above, or with the LGoodDatePicker library as shown here. (This is an alternative solution for the same problem.)

Fair disclosure: I'm the primary LGoodDatePicker developer.

The LGoodDatePicker library includes three TableEditor classes. These classes allow the programmer to add a DatePicker, a TimePicker, or a DateTimePicker, to the cells of a Swing JTable (or to a SwingX JXTable).

The picker classes can also be added to normal swing panels or other swing containers.

Here is an example of how to add a DateTimePicker to your JTable:

// Create a table.
JTable table = new JTable(new DemoTableModel());

// Add the DateTimeTableEditor as the default editor and renderer for
// the LocalDateTime data type.
table.setDefaultEditor(LocalDateTime.class, new DateTimeTableEditor());
table.setDefaultRenderer(LocalDateTime.class, new DateTimeTableEditor());

// Explicitly set the default editor and renderer for column index 0.
TableColumn column = table.getColumnModel().getColumn(0);
column.setCellEditor(table.getDefaultEditor(LocalDateTime.class));
column.setCellRenderer(table.getDefaultRenderer(LocalDateTime.class));

Here is an Oracle tutorial about How to use table editors.

I've pasted screenshots below of the table editor demo, the picker components, and the full demo. Note that the LGoodDatePicker library includes a separate demo for the table editors. It's in the Repository under this folder: "LGoodDatePicker/Project/src/main/java/com/github/lgooddatepicker/demo/TableEditorsDemo.java".

The library can be installed into your Java project from the project Release Page.

The project home page is on Github at:
https://github.com/LGoodDatePicker/LGoodDatePicker .

. Table Editors Demo screenshot

Date and TimePicker screenshots

Full Demo screenshot

Community
  • 1
  • 1
BlakeTNC
  • 941
  • 11
  • 14