简体   繁体   English

如何在JTable中添加JCheckBox

[英]How to add a JCheckBox in JTable

I stored my CheckBox in a vector and added it into table's row but it doesn't appears as a CheckBox in the table. 我将CheckBox存储在向量中,并将其添加到表的行中,但在表中未显示为CheckBox。

Here's the code: 这是代码:

    columns =new Vector();
    columns.add("<html><b>DRUG NAME");
    columns.add("<html><b>BRAND NAME");
    columns.add("<html><b>SELECT");

    chkMedicines=new JCheckBox();
    rows=new Vector();
    rows.add("CisPlatin");
    rows.add("Platinol-AQ");
    rows.add(chkMedicines);
    rows.add("Carboplatin");
    rows.add("Paraplatin");
    rows.add(chkMedicines);

    tblModel=new DefaultTableModel();
    tblModel.setColumnIdentifiers(columns);
    tblMedicines=new JTable(tblModel);
    tblModel.addRow(rows);
    add(new JScrollPane(tblMedicines));
    add(chkMedicines);

To make a JTable renders a boolean as a checkbox in the table cell we need to tell the table that a cell stores a boolean type of data. 为了使JTable在表单元格中将布尔值呈现为复选框,我们需要告诉表单元格存储了布尔类型的数据。 To do this we have to implement a TableModel for the JTable component. 为此,我们必须为JTable组件实现TableModel。

import javax.swing.*;
import javax.swing.table.AbstractTableModel;

import java.awt.*;

public class JTableBooleanAsCheckbox extends JPanel {

public JTableBooleanAsCheckbox() {
    initializeUI();
}

private void initializeUI() {
    setLayout(new BorderLayout());
    setPreferredSize(new Dimension(450, 150));

    JTable table = new JTable(new BooleanTableModel());
    table.setFillsViewportHeight(true);
    JScrollPane pane = new JScrollPane(table);
    add(pane, BorderLayout.CENTER);
}

public static void showFrame() {
    JPanel panel = new JTableBooleanAsCheckbox();
    panel.setOpaque(true);

    JFrame frame = new JFrame("JTable Boolean as Checkbox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(panel);
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JTableBooleanAsCheckbox.showFrame();
        }
    });
}

class BooleanTableModel extends AbstractTableModel {
    String[] columns = {"STUDENT ID", "NAME", "SCORE", "PASSED"};
    Object[][] data = {
            {"S001", "ALICE", 90.00, Boolean.TRUE},
            {"S002", "BOB", 45.50, Boolean.FALSE},
            {"S003", "CAROL", 60.00, Boolean.FALSE},
            {"S004", "MALLORY", 75.80, Boolean.TRUE}
    };

    public int getRowCount() {
        return data.length;
    }

    public int getColumnCount() {
        return columns.length;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        return data[rowIndex][columnIndex];
    }

    @Override
    public String getColumnName(int column) {
        return columns[column];
    }

    //
    // This method is used by the JTable to define the default
    // renderer or editor for each cell. For example if you have
    // a boolean data it will be rendered as a check box. A
    // number value is right aligned.
    //
    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return data[0][columnIndex].getClass();
    }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM