JTable中的SpinnerDateModel

时间:2015-08-18 20:30:51

标签: java swing date jtable jspinner

使用各种来源的几个例子我已经开发了基于Spinner的JTable编辑器。在我用其他类型的数据在其他列中按某些内容之前,一切都运行良好和花花公子。然后表冻结和控制台抛出IllegalArgumentException。我认为问题在于 getTableCellEditorComponent()方法以及以某种方式传递给Spinner的非法值。但是,我没有完全掌握这个机制。有人可以解释问题所在以及如何解决问题吗?

public class SpinnerEditor extends AbstractCellEditor implements TableCellEditor, TableCellRenderer{
private JSpinner editSpinner, renderSpinner;
private JTable table;
private String[] list;
private Border originalBorder;

public SpinnerEditor(JTable table, int column, Font font){

    SpinnerDateModel hourModel = new SpinnerDateModel();  
    editSpinner = new JSpinner(hourModel);
    editSpinner.setEditor(new JSpinner.DateEditor(editSpinner, "HH:mm"));
    editSpinner.setFont(font);
    renderSpinner = new JSpinner(hourModel);
    renderSpinner.setEditor(new JSpinner.DateEditor(renderSpinner, "HH:mm"));
    originalBorder = editSpinner.getBorder();
    editSpinner.setBorder(new LineBorder(Color.BLUE));
    this.table = table;
    table.getColumnModel().getColumn(column).setCellEditor(this);
}

public Object getCellEditorValue(){

    Date date=(Date)editSpinner.getValue();
    SimpleDateFormat format=new SimpleDateFormat("HH:mm");
    return format.format(date);
}

public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column){
    SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
    String dateInString = "11-03-1990 13:00:00";
    Date date = null;
    try {
        date = sdf.parse(dateInString);
    } catch (ParseException e) {
        e.printStackTrace();
    }   
     if (value == null) {
         editSpinner.setValue(date);
     }
    if (value != null) editSpinner.setValue(value);
    return editSpinner;
}

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column){
     return renderSpinner;
}

public boolean isCellEditable(EventObject evt){
    return true;
}
}

0 个答案:

没有答案