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

荒凉一梦 提交于 2019-12-13 03:48:35

问题


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


回答1:


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

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);
            }
        });
    }
}



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/7183503/how-would-i-go-about-changing-the-boolean-string-to-a-jcheckbox-in-jtable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!