0

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!!???

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Dylan Wheeler
  • 6,928
  • 14
  • 56
  • 80
  • No idea what you are doing wrong since you didn't post your SSCCE. 3 lines of code doesn't help us. – camickr Aug 25 '11 at 00:33

3 Answers3

3

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);
            }
        });
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
1

I ran across a similar problem while examining this (flawed) example. The method invocation getDefaultRenderer(Boolean.class) returns a JTable.BooleanEditor irrespective of the result returned by getColumnClass(), which is java.lang.Object for the unalloyed DefaultTableModel. To see the effect, drag the checkbox column to a new location and click the checkbox header. The new column zero in the view will now alternate between true and false.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0
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;
    }
}

I got this issue solved using the answer given by MKorbel.

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
Majid
  • 1