Java Swing, Trying to replace boolean check-box in a JTable with an image-icon checkbox

前端 未结 3 1874
忘了有多久
忘了有多久 2021-01-22 08:26

So I have a JTable with check-boxes. I would like to have the check-boxes contain one image when they are \"checked\" and another image when they are \"unchecked\" (i.e., displa

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-22 09:07

    Assuming that you have just a normal JTable you may set appropriate icons in renderer and editor:

    public void setIcons(Jtable table, int column, Icon icon, Icon selectedIcon) {
        JCheckBox cellRenderer = (JCheckBox) table.getCellRenderer(0, column);
        cellRenderer.setSelectedIcon(selectedIcon);
        cellRenderer.setIcon(icon);
    
        DefaultCellEditor cellEditor = (DefaultCellEditor) table.getCellEditor(0, column);
        JCheckBox editorComponent = (JCheckBox) cellEditor.getComponent();
        editorComponent.setSelectedIcon(selectedIcon);
        editorComponent.setIcon(icon);
    }
    

提交回复
热议问题