2014-10-02 59 views
0

我想創建一個CRUD-應用程序,我的問題是我不知道如何爲JTable編寫DateEditor。JTable中util.Date的DateEditor

我不知道如何實現DateEditor。格式應該是「dd.mm.yyyy」。

我希望你能幫助我。

以下是我開發和工作的東西。

的TableModel

public class SheepTableModel extends AbstractTableModel{  
    ArrayList<Sheep> data = new ArrayList(); 
    private String[] columnNames; 
    private SimpleDateFormat dateFormat = new SimpleDateFormat("dd.mm.yyyy"); 

    public SheepTableModel() { 

     data.add(new Sheep(1, "Test", new Date(2014, 6, 7), "ADSF", "sdfwe", new Date(2014, 8, 27), 1, new Date(2014, 8, 27), "Testnotiz")); 
     data.add(new Sheep(1, "Test", new Date(2014, 6, 7), "ADSF", "sdfwe", new Date(2014, 8, 27), 1, new Date(2014, 8, 27), "Testnotiz")); 
     data.add(new Sheep(1, "Test", new Date(2014, 6, 7), "ADSF", "sdfwe", new Date(2014, 8, 27), 1, new Date(2014, 8, 27), "Testnotiz")); 

     columnNames = new String[]{"Nummer", "Ohrenmarkennummer", "Geburtsdatum", "Nummer Mutter", "Nummer Vater", "DKV Datum", "Anmerkungsnummer", "Anmerkungsdatum", "Notiz"}; 
    } 

    @Override 
    public int getRowCount() { 
     return data.size(); 
    } 

    @Override 
    public int getColumnCount() { 
     return 9; 
    } 

    @Override 
    public Object getValueAt(int rowIndex, int columnIndex) { 
     Sheep s = (Sheep) data.get(rowIndex); 
     switch(columnIndex){ 
      case 0: return s.getSheepnumber(); 
      case 1: return s.getEarnumber(); 
      case 2: return s.getBirthdate(); 
      case 3: return s.getNr_mum(); 
      case 4: return s.getNr_dad(); 
      case 5: return s.getDkvdate(); 
      case 6: return s.getNotenr(); 
      case 7: return s.getNote_date(); 
      case 8: return s.getNote_addtext(); 
      default: return null; 
     } 

    } 

    @Override 
    public boolean isCellEditable(int row, int col){ 
     return col != 0; 
    } 

    @Override 
    public void setValueAt(Object value, int row, int col) { 
      switch(col){ 
       case 1: data.get(row).setEarnumber(((String) (value))); break; 
       case 2: data.get(row).setBirthdate(((Date) (value)));break; 
       case 3: data.get(row).setNr_mum(((String) (value)));break; 
       case 4: data.get(row).setNr_dad(((String) (value)));break; 
       case 5: data.get(row).setDkvdate(((Date) (value)));break; 
       case 6: data.get(row).setNotenr(((Integer)(value)));break; 
       case 7: data.get(row).setNote_date(((Date)(value)));break; 
       case 8: data.get(row).setNote_addtext(((String) (value))); 
      } 
      fireTableCellUpdated(row, col); 
    } 

    @Override 
    public String getColumnName(int columnIndex){ 
     return columnNames[columnIndex]; 
    } 

    @Override 
    public Class<?> getColumnClass(int columnIndex) { 
     switch(columnIndex){ 
      case 0: return int.class; 
      case 1: return String.class; 
      case 2: return Date.class; 
      case 3: return String.class; 
      case 4: return String.class; 
      case 5: return Date.class; 
      case 6: return int.class; 
      case 7: return Date.class; 
      case 8: return String.class; 
     } 
     return Object.class; 

    } 

} 

DateRenderer

public class DateCellRenderer extends DefaultTableCellRenderer{ 

    @Override 
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column){ 
     super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); 
     if (value instanceof Date){ 
      // Use SimpleDateFormat class to get a formatted String from Date object. 
      String strDate = new SimpleDateFormat("dd.mm.yyyy").format((Date)value); 
      // Sorting algorithm will work with model value. So you dont need to worry about the renderer's display value. 
      this.setText(strDate); 
    } 

    return this; 
    } 
} 
+1

[JCalendar](http://toedter.com/jcalendar/)是一個選項嗎? – Compass 2014-10-02 19:21:14

回答

3

你需要做兩件事情。

  1. 創建一個渲染器。檢查出Table Format Render一個簡單的方法來做到這一點。注意你不能只使用"dd.mm.yyyy"作爲格式,因爲上/下字符意味着不同的東西。請閱讀SimpleDateFormat API以瞭解要使用的正確格式字符。

  2. 您需要創建一個自定義編輯器來編輯日期。我想你應該能夠使用DefaultCellEditorJFormattedTextField使用適當的面具。有關JFormattedTextField的更多信息,請閱讀How to Use Formatted Text Fields上的Swing教程部分。

基本的代碼可能是這樣的:

MaskFormatter format = new MaskFormatter(...); 
JFormattedTextField ftf = new JFormattedTextField(format); 
DefaultCellEditor dce = new DefaultCellEditor(ftf); 
table.getColumnModel().getColumn(...).setCellEditor(dce); 

Swing的教程也有How to Use Tables一個部分,其提供更多的信息,渲染和編輯。