编程语言
首页 > 编程语言> > java-具有可编辑复选框的JTable

java-具有可编辑复选框的JTable

作者:互联网

下面的代码是我的项目类之一.当我单击“查找”按钮时,它将使用JTable生成框架,一些数据将动态加载到表中.表的最后一列必须是带事件的复选框.
我尝试了此代码的复选框(从另一个项目..它不起作用)

DefaultTableModel   dtm = new DefaultTableModel(data, colName){
         public Class getColumnClass(int c) {
                return (c == 5? Boolean.class : String.class);
            } 
        };

实际班

public class BookReturnPanel {
    JPanel retunBookPanel;
    JTextField txtRegNo;
    JButton btnFind, btnSave;
JTable retunTable = null;
    public JScrollPane jScrollPane = null;
    private int i;
    static Object[][] data;
    String regNo = null;
    Member member = null;
    DefaultTableModel model = new DefaultTableModel();
    /**
     * Create the panel.
     */
    public BookReturnPanel() {

    }

    public JPanel getRetunBookPanel() {
        if (retunBookPanel == null) {
            retunBookPanel = new JPanel();
            retunBookPanel.setLayout(null);

            model.addColumn("Member");
            model.addColumn("Book");
            model.addColumn("Issue Date");
            model.addColumn("Return Date");
            model.addColumn("Return");

            retunTable=new JTable(model);
            retunTable.setLocation(new Point(0,60));
            retunTable.setSize(new Dimension(517, 386));

            JLabel lblRegNo = new JLabel("Member Reg No:");
            lblRegNo.setBounds(24, 40, 108, 14);
            retunBookPanel.add(lblRegNo);
            retunBookPanel.add(getJScrollPane(),  BorderLayout.CENTER);
            txtRegNo = new JTextField();
            txtRegNo.setBounds(129, 37, 200, 20);
            retunBookPanel.add(txtRegNo);
            txtRegNo.setColumns(10);

            btnFind = new JButton("Find");
            btnFind.setBounds(359, 36, 91, 23);
            btnFind.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {

                    if (model.getRowCount() > 0) {
                        for (int i = model.getRowCount() - 1; i > -1; i--) {
                            model.removeRow(i);
                        }
                    }

                    regNo = txtRegNo.getText();
                    member = ServiceFactory.getMemberServiceImpl().findByregNo(regNo);


                    List<Issue> issues = ServiceFactory.getIssueServiceImpl()
                            .FindAllIssueByMemberId(member.getSerialNo());



                    for(Issue issue:issues){
                        Vector<Object>row=new Vector<Object>();

                        row.addElement( issue.getMemberId().getName());
                        row.addElement( issue.getBookId().getName());
                        row.addElement( issue.getIssueDate());
                        row.addElement( issue.getReturnDate());
                        row.addElement(issue.getStatus());

                        model.addRow(row);

                    }


                }
            });
            retunBookPanel.add(btnFind);

            btnSave = new JButton("Save");
            btnSave.setBounds(425, 448, 91, 23);
            retunBookPanel.add(btnSave);

        }
        return retunBookPanel;

    }

    private JScrollPane getJScrollPane() {

        if (jScrollPane == null) {
            jScrollPane = new JScrollPane();
            jScrollPane.setBounds(new Rectangle(0, 60, 517, 386));


            jScrollPane.setViewportView(retunTable);
        }
        return jScrollPane;
    }
}

上面的代码完美地产生了JTable,但是我需要在最后一列中显示JCheckbox.
当单击复选框时,还需要添加事件.

解决方法:

1)JTable列索引从0开始,如果要将JCheckBox设置为最后一列,则需要使用索引4,而不是代码中的5.

2)要向您的JCheckBox列添加操作,可以将DefaultCellEditor与JCheckBox一起使用,例如:

JCheckBox chb = new JCheckBox();
chb.addItemListener(new ItemListener() {

    @Override
    public void itemStateChanged(ItemEvent e) {
            System.out.println("action");
    }
});
DefaultCellEditor editor = new DefaultCellEditor(chb) {
    @Override
    public Component getTableCellEditorComponent(JTable table,Object value, boolean isSelected, int row,int column) {
           Component tableCellEditorComponent = super.getTableCellEditorComponent(table, value, isSelected, row, column);
           ((JCheckBox)tableCellEditorComponent).setHorizontalAlignment(JCheckBox.CENTER);
           return tableCellEditorComponent;
    }
};
retunTable.getColumnModel().getColumn(4).setCellEditor(editor);

标签:jcheckbox,java,swing,jtable
来源: https://codeday.me/bug/20191013/1911218.html