Search code examples
javaswingjtablebooleanjcheckbox

How would I go about changing the boolean string to a JCheckBox in JTable?


Alright. I have implemented a custom JTable model that includes the whole

@Override
public Class<?> getColumnClass(final int column) {

and inside that I have

if (column == 0)
    return Boolean.class;

When I run, and I go to the JTable, instead of a checked JCheckbox, it says true. When I click on it, it turns into a JCheckBox until I unclick and then either says true or false.

WHAT AM I DOING WRONG!!???


Solution

  • really don't know, you can compare your code with

    note both definitions for Column.Class are valid, you can try it that with uncomment blocked code

    enter image description here

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.table.*;
    
    public class TablePrepareRenderer extends JFrame {
    
        private static final long serialVersionUID = 1L;
        private JTable table;
    
        public TablePrepareRenderer() {
            Object[] columnNames = {"Type", "Company", "Shares", "Price", "Boolean"};
            Object[][] data = {
                {"Buy", "IBM", new Integer(1000), new Double(80.50), false},
                {"Sell", "MicroSoft", new Integer(2000), new Double(6.25), true},
                {"Sell", "Apple", new Integer(3000), new Double(7.35), true},
                {"Buy", "Nortel", new Integer(4000), new Double(20.00), false}
            };
            DefaultTableModel model = new DefaultTableModel(data, columnNames);
            table = new JTable(model) {
    
                private static final long serialVersionUID = 1L;
    
                /*@Override
                public Class getColumnClass(int column) {
                return getValueAt(0, column).getClass();
                }*/
                @Override
                public Class getColumnClass(int column) {
                    switch (column) {
                        case 0:
                            return String.class;
                        case 1:
                            return String.class;
                        case 2:
                            return Integer.class;
                        case 3:
                            return Double.class;
                        default:
                            return Boolean.class;
                    }
                }
    
                @Override
                public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
                    Component c = super.prepareRenderer(renderer, row, column);
                    int firstRow = 0;
                    int lastRow = table.getRowCount() - 1;
                    if (row == lastRow) {
                        ((JComponent) c).setBackground(Color.red);
                    } else if (row == firstRow) {
                        ((JComponent) c).setBackground(Color.blue);
                    } else {
                        ((JComponent) c).setBackground(table.getBackground());
                    }
                    return c;
                }
            };
            table.setPreferredScrollableViewportSize(table.getPreferredSize());
            JScrollPane scrollPane = new JScrollPane(table);
            getContentPane().add(scrollPane);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    TablePrepareRenderer frame = new TablePrepareRenderer();
                    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    }