我将如何将布尔字符串更改为 JTable 中的 JCheckBox?

发布于 2024-12-01 15:49:49 字数 401 浏览 0 评论 0 原文

好吧。我已经实现了一个自定义 JTable 模型,其中包括我运行时的整体

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

和内部

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

,并且我转到 JTable,而不是选中的 JCheckbox< /代码>,它说的是真的。当我单击它时,它会变成一个 JCheckBox ,直到我取消单击,然后才会显示 true 或 false。

我做错了什么!!???

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

楠木可依 2024-12-08 15:49:49

真的不知道,您可以比较您的代码,

注意 Column.Class 的两个定义都是有效的,您可以尝试使用取消注释阻止的代码

在此输入图像描述

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

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);
            }
        });
    }
}
失去的东西太少 2024-12-08 15:49:49

我在检查这个(有缺陷)时遇到了类似的问题getDefaultRenderer(Boolean.class) 返回一个 JTable.BooleanEditor,无论返回的结果如何getColumnClass(),它是非合金 DefaultTableModeljava.lang.Object。要查看效果,请将复选框列拖动到新位置,然后单击复选框标题。视图中的新零列现在将在 truefalse 之间交替。

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.

死开点丶别碍眼 2024-12-08 15:49:49
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;
    }
}

我使用 MKorbel 给出的答案解决了这个问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文