带有可编辑复选框的JTable

时间:2014-02-17 10:20:02

标签: java swing jtable jcheckbox

下面的代码是我的项目类之一。当我点击查找按钮时,它会生成一个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。 还需要在单击复选框时添加事件..!

2 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分:1)

@ user3318622:如果要在最后一个列单元格和TableCellEditor中显示您需要处理TableCellRenderer的复选框,则需要使用TableCellEditorItemListener复选框。