Link to home
Start Free TrialLog in
Avatar of Aurii
Aurii

asked on

Set CheckBox in JTable

I am using JTable in my application and developing application in Net beans IDE. I dragged n drop JTable in my form but doing code manually like set model, set rows and columns & initialize it, etc.
Now I want to set CheckBox in table cells. How to set it?
Actually I wrote code for same but it’s not showing CheckBox in cells. Its showing ‘true’, ’false’ instead. I want that tick box in cells. How to do it? I want to set checkBox to columns from column 4 to column 34.
My code is below,

private String rows[][]= new String[1][0];
    private String col[]={"v-ua-","laiw.kZ ukao","gqn~nk","xko","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31",",dw.k gtj fnol","nj",",dw.k ixkj #i;s","n22ndex"};
    private javax.swing.table.DefaultTableModel tm = new javax.swing.table.DefaultTableModel(rows,col){
    public boolean isCellEditable(int row, int col)
    {
       if(col==0 || col==1 || col==2 || col==3 || col==35 || col==36 || col==37 || col==38)
       {return false;}
        else
       {return true;}
    }
    };

myTable.setModel(tm);
JCheckBox chkbx = new JCheckBox();
        for(int i=4;i<35;i++)
        {
            TableColumn formatColumn = tblSearch.getColumnModel().getColumn(i);
            formatColumn.setCellEditor(new DefaultCellEditor(chkbx));
        }

Please suggest solution for my code. PLEASE HELP.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You need to override getColumnClass too and return Boolean.class for the appropriate col
Avatar of Aurii
Aurii

ASKER

but how?
(and that's all you need to do)
Avatar of Aurii

ASKER

tell me it by code (in my code)
   private javax.swing.table.DefaultTableModel tm = new javax.swing.table.DefaultTableModel(rows,col){
    public boolean isColumnClass(int col)
    {
       if(col==0 || col==1 || col==2 || col==3 || col==35 || col==36 || col==37 || col==38)
       {return Boolean.class;}
        else
       {return Object.class;}
    }
    public boolean isCellEditable(int row, int col)
    {
       if(col==0 || col==1 || col==2 || col==3 || col==35 || col==36 || col==37 || col==38)
       {return false;}
        else
       {return true;}
    }
    };
and add Booleans to your model where you want checkboxes
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
:)