JTable Column not editable after custom renderer

落爺英雄遲暮 提交于 2019-11-26 18:38:55

问题


This is a follow up to the question posted here.

I've followed the instructions in the answer, and it worked like a charm. However, the date column cannot be edited: I cannot select any JDateChooser within the populated table.

As mentioned in the earlier question, I'm using an MVC pattern. When I populate the JTable and setCellRenderer from within the view, it works fine. It is only when I populate and setCellRenderer from my controller that the date column isn't enabled.

Here is the 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);
    }
    this.setEnabled(true);
    return this;
}

Here is the code from my view which works:

scrollPanePermits = new JScrollPane();
    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();
         }
        @Override
        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());

Finally, this is the code from my controller that populates the table, but the date column is not editable:

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());

//      TableColumn dateColumn = gui.getTableVehiclePermit().getColumnModel().getColumn(1);
//      dateColumn.setCellRenderer(new JDateChooserRenderer());

        gui.getTableVehiclePermit().getColumn("Expiration Date").setCellRenderer(new JDateChooserRenderer());

        gui.getTableVehiclePermit().setEnabled(true);

where gui is an instance of my view.

I only know of overriding the isCellEditable method, when creating the table, in order to set cells as editable or not. Any ideas on how to go about this, or what I'm doing wrong?

Any help would be greatly appreciated.


回答1:


It might help to start with a test of com.toedter.calendar.demo.DemoTable, shown below. Note how the line

table.setDefaultEditor(Date.class, new JDateChooserCellEditor());

specifies the default editor for model values of type Date.class:

@Override
public Class getColumnClass(int c) {
    return getValueAt(0, 1).getClass();
}

import com.toedter.calendar.demo.DemoTable;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JFrame;

/** @see http://stackoverflow.com/a/14880675/230513 */
public class TableTest {

    private void display() {
        JFrame f = new JFrame("TableTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new DemoTable());
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TableTest().display();
            }
        });
    }
}


来源:https://stackoverflow.com/questions/14870778/jtable-column-not-editable-after-custom-renderer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!