问题
Excuses for asking the same question again. I am under the impression that if I added to the original question, I wouldn't get as many views.
As I'm using an MVC pattern, my Swing GUI is in a different class with the controller creating an instance of it. Here is how I create the table in my View:
tableVehiclePermitHeader = new String[] {"Name", "Expiration Date"};
tableVehiclePermitData = new Object[0][0];
// Calendar tempDate = new GregorianCalendar(2008, 1, 1);
// Date tempDate1 = new Date(2008, 1, 1);
// tempDate1.setYear(tempDate1.getYear() - 1900);
// tableVehiclePermitData = new Object [][] {{"Hello", tempDate}, {"Hello", tempDate1}};
tableVehiclePermitDefaultTableModel = new DefaultTableModel(tableVehiclePermitData, tableVehiclePermitHeader);
tableVehiclePermit = new JTable(tableVehiclePermitDefaultTableModel){
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int rowIndex, int colIndex){
if (colIndex == 0){
return false;
} else {
return true;
}
}
};
tableVehiclePermit.getColumn("Expiration Date").setCellRenderer(new JDateChooserRenderer());
tableVehiclePermit.getColumn("Expiration Date").setCellEditor(new JDateChooserCellEditor());
The commented lines show the JDateChooser correctly. It is only when I populate the table content from my view that I'm unable to render dates as JDateChoosers.
Below is my renderer:
public class JDateChooserRenderer extends JDateChooser implements TableCellRenderer{
Date inDate;
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
// TODO Auto-generated method stub
if (value instanceof Date){
this.setDate((Date) value);
} else if (value instanceof Calendar){
this.setCalendar((Calendar) value);
}
return this;
}
}
Finally, my Controller where I get the data to populate the table from a database as an ArrayList of a defined type that has the date fields as type java.util.Date (also tried java.sql.Date to the same result).
ArrayList<VehiclePermitExpirationByVehicleDao> vehPermit = vpmodel.permitExpirationPerVehicle(id);
VehiclePermitExpirationByVehicleDao permitData;
permitListData = new Object[vehPermit.size()][3];
Iterator it = vehPermit.iterator();
int i = 0;
while (it.hasNext()){
permitData = (VehiclePermitExpirationByVehicleDao) it.next();
permitListData[i][0] = permitData.getVehiclePermitName();
permitListData[i][1] = permitData.getExpirationDate();
permitListData[i][2] = permitData.getVehiclePermitId();
i++;
}
gui.setTableVehiclePermitData(permitListData);
gui.getTableVehiclePermitDefaultTableModel().setDataVector(gui.getTableVehiclePermitData(), gui.getTableVehiclePermitHeader());
gui.getTableVehiclePermit().setModel(gui.getTableVehiclePermitDefaultTableModel());
gui.getTableVehiclePermit().setEnabled(true);
Can someone please advise what I'm doing wrong? My suspicion is the type of the column holding the dates.
Also, if anyone had an idea how to go about the editor, kindly let me know.
Regards.
回答1:
You should get TableColumn for the Date column using columnIndex and then set the Renderer and Editor on the TableColumn.
TableColumn column1 = tableVehiclePermit.getColumnModel().getColumn(1);
column1.setCellRenderer(new JDateChooserRenderer());
column1.setCellEditor(new JDateChooserCellEditor());
来源:https://stackoverflow.com/questions/14835742/rendering-toedters-jdatechooser-in-jtable-column