Insert Radiobuttons in JTable Netbeans

非 Y 不嫁゛ 提交于 2019-12-09 02:20:22

You have to implement TableCellEditor and override getTableCellEditorComponent like this

public java.awt.Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)

{
            return new JRadioButton();
}

Hope this helps

EDIT: You can write like this

this.jTable1.getColumnModel().getColumn(0).setCellEditor(new javax.swing.table.TableCellEditor(){
    @Override
    public java.awt.Component getTableCellEditorComponent(javax.swing.JTable table, java.lang.Object value, boolean isSelected, int row, int column) {
        return new javax.swing.JRadioButton();
    }

    @Override
    public java.lang.Object getCellEditorValue() {
return null;//Here you can return the radio button status
    }

    @Override
    public boolean isCellEditable(java.util.EventObject anEvent) {
        return false; //uneditable cell
    }

    @Override
    public boolean shouldSelectCell(java.util.EventObject anEvent) {
        return false;
    }

    @Override
    public boolean stopCellEditing() {
        return true;
    }

    @Override
    public void cancelCellEditing() {       
    }

    @Override
    public void addCellEditorListener(javax.swing.event.CellEditorListener l) {
    }

    @Override
    public void removeCellEditorListener(javax.swing.event.CellEditorListener l) {
    }

});

You can take out the cell editor implementation to a separate class.

Also you have to set the flags accordingly to your requirement.

I'd suggest

  1. change JRadioButton to JCheckBox, because JCheckBox in the JTable respresents only Boolean value

  2. don't use generated code from NetBeans, write code by your hands

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