how to set renderer to checkbox on jtable for row?

风格不统一 提交于 2020-01-25 04:48:08

问题


  1. I set the renderer to the checkbox on jtable using following code

    Object[] ColumnData = {"Sr No","Ward Name","Total voters","Action"};
    Object[][] RawData=null;
    

    // in loop

    model.insertRow(x, new Object[]{ key,ward_name_var,total_vot_var,new Object[]{o}}); model.setValueAt(o,x,3); tblWard.setModel(model);

    Setchk(tblWard,3,checkbox); // by calling this method which contains following

    private void Setchk(JTable jTable1, int i, JCheckBox checkbox) { jTable1.getColumnModel().getColumn(i).setCellRenderer((new CWCheckBoxRenderer())); jTable1.getColumnModel().getColumn(i).setCellEditor(new CheckBoxCellEditor()); }

Blockquote

how can we try it for row to set the checkbox on jtable. thanks in advance.


回答1:


If your data is of type Boolean.class, the default render will display a checkbox. To change the checkbox in a particular row, you need a corresponding CellEditor. The default render/editor are used here; custom components are illustrated here.




回答2:


You can simply override the getCellRenderer method of your JTable to return the desired renderer for a given row. Example:

JTable table = new JTable() {
    TableCellRenderer getCellRenderer(int row, int column) {
        if (row == checkBoxRow)
            return myCheckBoxRenderer;
        else
            return super.getCellRenderer(row, column);
    }
};


来源:https://stackoverflow.com/questions/9905362/how-to-set-renderer-to-checkbox-on-jtable-for-row

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