[英]CellEditor with JSpinner Time format reverts to date format
I'm trying to create a DefaultCellEditor
for my JTable
's column so that I can set the start time and end time of a schedule. 我正在尝试为JTable
的列创建一个DefaultCellEditor
,以便可以设置计划的开始时间和结束时间。 However, I can't figure out why is it that when I input the time using the JSpinner
in JTable
's cell, I see the correct formatting for picking time but when I press enter or deselects the current cell, I get an entire date instead of just the time . 但是,我不知道为什么在JTable
的单元格中使用JSpinner
输入时间时,会看到正确的选择时间格式,但是当我按Enter或取消选择当前单元格时, 会得到一个完整的日期不仅仅是时间 。
Here's my DefaultCellEditor
with JSpinner
. 这是我的带有JSpinner
的DefaultCellEditor
。
public class ScheduleCellEditor extends DefaultCellEditor {
private final JSpinner timeSpinner;
public ScheduleCellEditor() {
super(new JTextField());
timeSpinner = new JSpinner();
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 24);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Date date = new Date();
SpinnerDateModel model = new SpinnerDateModel(date, null, null, Calendar.HOUR_OF_DAY);
model.setValue(calendar.getTime());
timeSpinner.setModel(model);
JSpinner.DateEditor editor = new JSpinner.DateEditor(timeSpinner, "HH:mm");
DateFormatter formatter = (DateFormatter) editor.getTextField().getFormatter();
formatter.setAllowsInvalid(false);
formatter.setOverwriteMode(true);
timeSpinner.setEditor(editor);
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
return timeSpinner;
}
@Override
public Object getCellEditorValue() {
return timeSpinner.getValue();
}
}
And here's how I set the DefaultCellEditor
to JTable
TableColumn
s 这是我将DefaultCellEditor
设置为JTable
TableColumn
的方法
TableColumnModel columnModel = jtblSchedule.getColumnModel();
TableColumn startTimeCol = columnModel.getColumn(1);
TableColumn endTimeCol = columnModel.getColumn(2);
startTimeCol.setCellEditor(new ScheduleCellEditor());
endTimeCol.setCellEditor(new ScheduleCellEditor());
When I click the cell and pick the time, it shows correct formatting where I only see HH:mm 当我单击单元格并选择时间时,它显示正确的格式,而我只看到HH:mm
When I click out of the cell or click somewhere else, I get the full date even when the editor and model is formatted to time. 当我从单元格中单击或单击其他位置时,即使将编辑器和模型格式化为时间格式,我也可以获得完整的日期。
Do you have any suggestions on how to solve this. 您对如何解决此问题有任何建议吗? I just need the time to show and not the complete date. 我只需要显示时间而不是完整日期。
I'll appreciate any help. 我将不胜感激。
Thanks. 谢谢。
Your TableCellEditor
seems to be in order, but you're still getting the default renderer for the time value. 您的TableCellEditor
似乎是有序的,但是您仍在获取时间值的默认渲染器 。 You'll need a custom renderer with a suitable SimpleDateFormat
. 您将需要一个具有合适SimpleDateFormat
的自定义渲染器 。 Adapted from this example , perhaps something like this: 从这个例子改编,也许是这样的:
private static final DateFormat TIME_FORMAT = DateFormat.getTimeInstance();
…
private class TimeRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean selected, boolean focused, int row, int column) {
super.getTableCellRendererComponent(table, value, selected, focused, row, column);
setText(TIME_FORMAT.format((Date) value));
return this;
}
}
…
TimeRenderer timeRenderer = new TimeRenderer();
startTimeCol.setCellRenderer(timeRenderer);
endTimeCol.setCellRenderer(timeRenderer);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.