How to make JTable column contain checkboxes?

后端 未结 5 2035
走了就别回头了
走了就别回头了 2020-12-20 15:53

Preface: I am horrible with java, and worse with java ui components.

I have found several different tutorials on how to add buttons to tables, however I am strugglin

相关标签:
5条回答
  • 2020-12-20 16:21

    Here's a simple rather elaborate example using a TableCellRenderer and TableCellEditor. See also, Concepts: Editors and Renderers.

    Addendum: @Jay Askren's point is well taken. The default renderer for Boolean.class, as described in the tutorial, may be all you need.

    0 讨论(0)
  • 2020-12-20 16:35

    The easiest solution is to use the DefaultTableModel and use Boolean object as values.

    0 讨论(0)
  • 2020-12-20 16:35

    In the Swing Designer set column type to boolean

    0 讨论(0)
  • 2020-12-20 16:44

    There's no need to create your own table renderer. Here's a simpler example. Just create a custom table model and for a given column return the class Boolean for:

    public Class getColumnClass(int column)
    

    If you want the column to be editable, return true for

    public boolean isCellEditable(int row, int column)
    

    JTable takes care of the rendering for you.

    Another example is here.

    0 讨论(0)
  • 2020-12-20 16:47

    As Peter say, its easy using extended DefaultTableModel class, ex:

    class NewTableModel extends DefaultTableModel{
            public Class<?> getColumnClass(int columnIndex) {
                return getValueAt(0, columnIndex).getClass();
            }
        }
    
    0 讨论(0)
提交回复
热议问题