2013-02-23 99 views
0

我想在JTable Cell中添加一个JDateChooser。 我知道如何添加文本框,组合框和复选框。JTable Cell中的JDatechooser

我允许用户输入数据到表中。用户应该能够为每一行选择一个日期。

任何帮助,将不胜感激。
谢谢

回答

4

阅读Swing教程Using Other Editors中的部分。

它显示了如何使用JColorChooser创建自定义编辑器。我猜想代码会类似于使用JDateChooser。

+1

@Prabhath:这[示例](http://stackoverflow.com/a/14880675/230513)使用[tag:jcalendar]可能会指导您。 – trashgod 2013-02-23 05:11:14

0

设置CellEditor的如下:

table.getColumnModel().getColumn(1).setCellEditor(new JDateChooserEditor(new JCheckBox())); 

/********* *******************************************/

这里是JDateChooserEditor代码:

class JDateChooserEditor extends DefaultCellEditor 
{ 
    public JDateChooserEditor(JCheckBox checkBox) 
    { 
    super(checkBox); 

    } 

    JDateChooser date; 
    public Component getTableCellEditorComponent(JTable table, Object value, 
     boolean isSelected, int row, int column) 
    { 

     date = new JDateChooser(); 
     date.setDateFormatString("dd-MM-yyyy"); 
     return date; 
    } 

    public Object getCellEditorValue() 
    { 
    return new String(((JTextField)date.getDateEditor().getUiComponent()).getText()); 
    } 

    public boolean stopCellEditing() 
    { 
    return super.stopCellEditing(); 
    } 

    protected void fireEditingStopped() { 
    super.fireEditingStopped(); 
    } 
} 

Screen Shot