我想創建一個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;
}
}
[JCalendar](http://toedter.com/jcalendar/)是一個選項嗎? – Compass 2014-10-02 19:21:14