3

Is it possible to add a DateTimePicker to a JTable Cell.A particular column should be updated with date and time..Is it possible to add such a component to a JTable

Harish
  • 3,343
  • 15
  • 54
  • 75

6 Answers6

4

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).

Fair disclosure: I'm the primary developer.

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));

I've pasted screenshot below of the table editor demo, the picker components, and the full demo. Note that the 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

BlakeTNC
  • 941
  • 11
  • 14
3

Yes it is. See this Swing Tutorial Track: http://download.oracle.com/javase/tutorial/uiswing/components/table.html#combobox

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
  • -300 because the Tutorial does not mention any Editor for Date and Time; just a Renderer for a Date. – bobndrew Aug 25 '10 at 13:44
  • 2
    +1, yes the tutorial doesn't give a specific example of a date editor, but it does show how to use a color chooser as an editor. So the concept will be the same. All the OP has to do is find a Date editor that they like to use. There are many date editors floating around on the web. – camickr Aug 25 '10 at 14:37
2

Try to use the FLib-JCalendar component as an CellEditor in a JTable.
(and post the working example here if you made it work)

bobndrew
  • 395
  • 10
  • 32
2

Yes, but you'll need to implement both TableCellRenderer and TableCellEditor. As suggested by @Jens Schauder, the tutorial may be helpful. You might also look at this tutorial based example using JCheckBox.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
2

Simply use this code to set DatePicker in column 0:

    TableColumn dateColumn = YOURTABLE.getColumnModel().getColumn(0);
    dateColumn.setCellEditor(new DatePickerCellEditor());
0

By using the premise of Jens link and applying that logic with the below link, you can add a date time picker into a JTable, just be careful, combining JCheckbox, JCombobox and the date time picker into a JTable the focus get a little messy, but i believe it can be solved with a focus listener, it will just take some time t implement.

I am currontly working on such a Table render-er with will incorperate it all, i will post it online when im done.

Link: date and time picker in JAVA

Community
  • 1
  • 1
diegeelvis_SA
  • 453
  • 3
  • 8