1

I know its a very basic question I have checkboxes in my tables's first column Problem is checkboxes are not clickable.I have searched for similar threads and tried to assign the Boolean editor to the column and to override the getColumnClass()But I couldnt do it This is what I have tried so far

String[] columnNames = {"Column 1", "Column 2"};  
DefaultTableModel model = new DefaultTableModel(columnNames, 0);  
table=new JTable(model );  
table.getColumnModel().getColumn(0).setCellRenderer( table.getDefaultRenderer(Boolean.class) );  

ArrayList<org.jsoup.nodes.Element >arr=GetrowCount("http://www.mit.edu/");
for (org.jsoup.nodes.Element element :  arr) {
    Object[]  rows = {Boolean.FALSE, element};  
    model.addRow( rows );
}

scrollPane=new JScrollPane(table);

panel.add(scrollPane);
this.add(panel);
this.setSize(300,300);
alex2410
  • 10,904
  • 3
  • 25
  • 41
user3733078
  • 249
  • 2
  • 11

2 Answers2

2

Your JCheckBox is not editable because you set Renderer for column, but for editing you need add Editor, read more about Editors and Renderers.

Another way is to override getColumnClass() method of TableModel and return Boolean.class for column:

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class TestFrame extends JFrame {

    public static void main(String... s) {
        new TestFrame();
    }

    public TestFrame() {
        init();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void init() {
        String[] columnNames = {"Column 1", "Column 2"};  
        DefaultTableModel model = new DefaultTableModel(columnNames, 0){
            @Override
            public Class<?> getColumnClass(int columnIndex) {
                return columnIndex == 0 ? Boolean.class : super.getColumnClass(columnIndex);
            }
        };  
        JTable table=new JTable(model );  

        for (int i =0;i<5;i++) {
            Object[]  rows = {Boolean.FALSE, i};  
            model.addRow( rows );
        }

        add(new JScrollPane(table));
    }
}
alex2410
  • 10,904
  • 3
  • 25
  • 41
1

You should use a custom table model that will return the checkbox column as editable. Check this link for more info and examples: https://stackoverflow.com/a/2901500/1843508

Community
  • 1
  • 1
Ofer Lando
  • 814
  • 7
  • 12
  • Yes I know that link I have checked it out before .But when I use custom model I am having trouble while adding rows.Is it possible to stick to code – user3733078 Jan 19 '15 at 12:03