在JTable中显示JCheckBox [英] Display JCheckBox in JTable

查看:98
本文介绍了在JTable中显示JCheckBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Jtable,我想在一个列中添加一个JCheckbox。但是,当我创建一个JCheckbox对象时,列中会显示javax.swing.JCheckBox。请参阅图像。你能告诉我如何修改吗?我到处搜索,但似乎无法找到任何解决方案。谢谢。

I have a Jtable in which I want to add a JCheckbox in one column. However, when I create a JCheckbox object, javax.swing.JCheckBox is being displayed in the column.Please refer to the image. Can you tell me how to amend that please? I have searched everywhere but cannot seem to find any solution for it. Thank you.

推荐答案


  1. 不要将组件添加到 TableModel ,这不是 TableModel的责任

  2. 您需要指定列的类类型。假设您正在使用 DefaultTableModel ,您可以简单地用一堆布尔填充列,这应该可行 - 测试后,您将需要覆盖 getColumnClass DefaultTableModel 的方法(或任何 TableModel 实现)并确保对于复选框列,它返回 Boolean.class

  1. don't add components to your TableModel, that's not the responsibility of the TableModel
  2. You will need to specify the class type of your column. Assuming you're using a DefaultTableModel, you can simply fill the column with a bunch of booleans and this should work - After testing, you will need to override the getColumnClass method of the DefaultTableModel (or what ever TableModel implementation) and make sure that for the "check box" column, it returns Boolean.class

有关更多详细信息,请参见如何使用表格

See How to use tables for more details

例如......

import java.awt.EventQueue;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

public class TestCardLayout {

    public static void main(String[] args) {
        new TestCardLayout();
    }

    public TestCardLayout() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                Random rnd = new Random();
                DefaultTableModel model = new DefaultTableModel(new Object[]{"Check boxes"}, 0) {

                    @Override
                    public Class<?> getColumnClass(int columnIndex) {
                        return Boolean.class;
                    }

                };
                for (int index = 0; index < 10; index++) {
                    model.addRow(new Object[]{rnd.nextBoolean()});
                }
                JTable table = new JTable(model);

                final JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new JScrollPane(table));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

这篇关于在JTable中显示JCheckBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆