java JTable 中的日期时间选择器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3562799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:25:46  来源:igfitidea点击:

Date time picker inside JTable

javaswingdatetimedatejtable

提问by Harish

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

是否可以将 DateTimePicker 添加到 JTable Cell.A 特定列应更新日期和时间..是否可以将此类组件添加到 JTable

采纳答案by Jens Schauder

回答by BlakeTNC

The LGoodDatePickerlibrary includes three TableEditorclasses. 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).

LGoodDatePicker库包括三个TableEditor类。这些类允许程序员将 DatePicker、TimePicker 或 DateTimePicker 添加到 Swing JTable(或 SwingX JXTable)的单元格中。

Fair disclosure: I'm the primary developer.

公平披露:我是主要开发人员。

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

Picker 类也可以添加到普通的秋千面板或其他秋千容器。

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

以下是如何将 DateTimePicker 添加到 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 Repositoryunder this folder: "LGoodDatePicker/Project/src/main/java/com/github/lgooddatepicker/demo/TableEditorsDemo.java".

我在下面粘贴了表格编辑器演示、选择器组件和完整演示的屏幕截图。请注意,该库包含一个单独的表格编辑器演示。它位于此文件夹下的存储库中:“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.

该库可以从项目发布页面安装到您的 Java 项目中。

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

项目主页位于 Github 上:
https: //github.com/LGoodDatePicker/LGoodDatePicker

. Table Editors Demo screenshot

. 表编辑器演示屏幕截图

Date and TimePicker screenshots

日期和时间选择器屏幕截图

Full Demo screenshot

Full Demo screenshot

回答by Nouman Nazir

Simply use this code to set DatePicker in column 0:

只需使用此代码在第 0 列中设置 DatePicker:

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

回答by bobndrew

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

尝试将FLib-JCalendar组件用作 JTable 中的 CellEditor。
(如果您成功了,请在此处发布工作示例)

回答by trashgod

Yes, but you'll need to implement both TableCellRendererand TableCellEditor. As suggested by @Jens Schauder, the tutorialmay be helpful. You might also look at this tutorialbased exampleusing JCheckBox.

是的,但您需要同时实现TableCellRendererTableCellEditor。正如@Jens Schauder 所建议的,本教程可能会有所帮助。你也可以看看这个教程基于例如使用JCheckBox

回答by diegeelvis_SA

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.

通过使用 Jens 链接的前提并通过下面的链接应用该逻辑,您可以将日期时间选择器添加到 JTable 中,但要小心,将 JCheckbox、JCombobox 和日期时间选择器组合到 JTable 中,焦点会变得有点混乱,但我相信它可以通过一个焦点监听器来解决,它只需要一些时间来实现。

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

链接:JAVA 中的日期和时间选择器