问题
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..
回答1:
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();
}
});
}
}
回答2:
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);
回答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
回答4:
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 .
.
来源:https://stackoverflow.com/questions/11822526/how-to-add-a-jxdatepicker-for-a-jtable-column