繁体   English   中英

禁用 JTable 上某些行上的复选框

[英]Disable checkbox on certain lines on a JTable

我在 Netbeans 8.2 项目上有一个 JTable ,它显示了一个列表的数据 class (我们称之为客户端)和每一行第一列的复选框。 我已经设置了这种方式来更改对象属性菜单中列的数据类型。 如果客户的属性为假,我如何不显示此复选框?

我假设第一列不可编辑。 如果要在单元格值为 false 时隐藏 JCheckBox,可以使用客户单元格渲染器:

private class MyCellRenderer extends DefaultTableCellRenderer
{

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col)
    {
        Component c;
        if (((Boolean)value).equals(true))
        {
            // Use the default renderer for Boolean which is JCheckBox based
            c = myTable.getDefaultRenderer(table.getColumnClass(col)).getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
        } else
        {
            // Use the standard default renderer which is a JLabel
            c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
            if (c instanceof JLabel)
            {
                ((JLabel) c).setText(null);
            }
        }
        return c;
    }
}

并将其应用于第一个 boolean 列:

myTable.getColumnModel().getColumn(0).setCellRenderer(new MyCellRenderer());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM