Checkbox in some cells but not all, in a particular column - JTable

前端 未结 3 1894
悲哀的现实
悲哀的现实 2021-01-15 02:55

This may be a vague query, so please pardon me.

Customized JTable (I\'ve modified the query and will discuss based on the SSCCE provided). I\'ve to create a JTable t

3条回答
  •  借酒劲吻你
    2021-01-15 03:18

    You can use the Boolean wrapper class instead of the raw type and write your own TableCellRenderer and override the getTableCellRendererComponent(..) method.

    public class CustomTableCellRenderer extends DefaultTableCellRenderer
    {
        public CustomTableCellRenderer()
        {
            super();
        }
    
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, 
                boolean isSelected, boolean hasFocus, int row, int column)
        {
            super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    
            if(value instanceof Boolean)
            {
                if(value != null)
                {
                     JCheckBox jcb = new JCheckBox();
                     jcb.setSelected((Boolean) value);
    
                     return jcb;
                }
                return new JPanel();
            }
        return this;
        }
    }
    

    Then just simply set infos.setDefaultRenderer(Boolean.class, new CustomTableCellRenderer()); and replace your raw types in the array with new Boolean(true). Wherever you don't want to have a JCeckBox you put a null instead of a Boolean and an empty JPanel will appear.

提交回复
热议问题