如何通过添加布尔列将JCheckBox添加到DefaultTableModel? [英] How do I add a JCheckBox to DefaultTableModel by adding a Boolean column?

查看:143
本文介绍了如何通过添加布尔列将JCheckBox添加到DefaultTableModel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 jcheckbox 添加到JTable的第一列,该列使用 DefaultTableModel 。我尝试为该列返回 Boolean.class ,但它不起作用。

I am trying to add a jcheckbox to the first column of a JTable, which uses a DefaultTableModel. I tried returning a Boolean.class for that column, but it doesn't work.

我在最后一列中已经有了一个 jcombobox ,但是我使用了相同的方法添加它以添加 jcheckbox 不起作用。我在网上看到,如果你在列中渲染 Boolean.class ,java会自动返回复选框,但是使用它也不起作用。我认为这可能是我订购组件的方式有问题。

I already have a jcombobox in the last column, but using the same method I used to add that in order to add a jcheckbox doesn't work. I read online that java automatically returns a checkbox for you if you render a Boolean.class in a column, but using it also doesn't work. I think this might be a problem in the way I am ordering the components.

//import statements. 
public class CourseSelection extends GUIDesign implements ActionListener{
    // lot of extraneous stuff,
    JLabel termLabel;
    String term;

    JLabel departmentLabel;
    String department;
    String[] columns = {
        "Select", "CRN", "Title", "Instructor", "Time", 
        "Days", "Location", "Section", "Code", "Mode of Grading"
    };
    int courses;
    Object[][] data;

    String[] modes = {"Audit", "Pass/Fail", "Letter Grding"};

    // create the table here, with model. 
    JTable table;

    DefaultTableModel model = new DefaultTableModel() {
        Class[] types = {
            Boolean.class, String.class, String.class, String.class, 
            String.class, String.class, String.class, String.class, 
            String.class, Object.class
        };
        // making sure that it returns boolean.class.   
        @Override
        public Class getColumnClass(int columnIndex) {
            return types[columnIndex];
        }
    };

    JPanel termPanel = new JPanel();
    JPanel departmentPanel = new JPanel();

    JButton backButton = new JButton("Back");
    JButton registerButton = new JButton("Register");
    JButton refreshButton = new JButton("Refresh");

    public CourseSelection(GTPort gPort) {
        super("Course Selection", 3);
        setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS));
        header = new JPanel();
        header.setSize(getWidth(), 30);
        body = new JPanel();
        body.setLayout(new BoxLayout(body,BoxLayout.Y_AXIS));

        header.setBackground(color);
        title.setFont(new Font("Helvetica", 1,20));
        header.add(title);

        termLabel = new JLabel("Term:        " + term);

        data = new Object[25][10];
        model = new DefaultTableModel(data, columns);
        table = new JTable(model);
        table.setModel(model);

        departmentLabel = new JLabel("Department: " + department);

        termPanel.add(termLabel);
        departmentPanel.add(departmentLabel);

        JComboBox box = new JComboBox(modes);

        JCheckBox checkBox = new JCheckBox();

        // lot of other code I tried that doesn't work. 
        // table.getColumnModel().getColumn(1)
        //      .setCellEditor(table.getDefaultEditor(Boolean.class));  
        // table.getColumnModel().getColumn(1)
        //      .setCellRenderer(table.getDefaultRenderer(Boolean.class));  

        // table.setDefaultEditor(Boolean.class, new DefaultCellEditor(checkBox));
        // table.setDefaultRenderer(Boolean.class, new DefaultTableCellRenderer());

        // model.getColumnModel().getColumn(1)
        //      .setCellEditor(new DefaultCellEditor(checkBox));
        // model.getColumnModel().getColumn(1)
        //      .setCellRenderer(new DefaultTableCellRenderer());

        table.getColumnModel().getColumn(9)
             .setCellEditor(new DefaultCellEditor(box));
        //   .setCellEditor(new DefaultCellEditor(box));

        // table = new JTable(model);

        body.add(termPanel);
        body.add(departmentPanel);

        body.add(table.getTableHeader());
        body.add(table);

        // body.add(body);
        buttonPanel.add(backButton);
        buttonPanel.add(refreshButton);
        buttonPanel.add(registerButton);

        backButton.addActionListener(this);
        refreshButton.addActionListener(this);
        registerButton.addActionListener(this);

        add(header);
        add(body);
        add(buttonPanel);

        gtPort = gPort;
    }
}


推荐答案

你不要在模型中覆盖 getColumnClass()

model = new DefaultTableModel(data, columns); // nothing overridden here
table = new JTable(model);
table.setModel(model);

覆盖它,它会起作用:

model = new DefaultTableModel(data, columns) {
    @Override
    public Class<?> getColumnClass(int column) {
        ...
    }
};
table = new JTable(model);
table.setModel(model);

这篇关于如何通过添加布尔列将JCheckBox添加到DefaultTableModel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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