简体   繁体   English

如何在可编辑的JTable中设置JCheckbox?

[英]How to set JCheckbox in JTable edditable?

I have JTable for displaying values from the user input. 我有JTable用于显示用户输入的值。 Using a class 'Employee' i take values from the user and display them within the table. 我使用“雇员”类从用户那里获取值并将其显示在表中。 I have used the methods getColumnClass() , setValueAt() and isCellEdditable . 我已经使用了getColumnClass()setValueAt()isCellEdditable方法 The result from my program displays the checkbox but does not allow me to tick the box: 我程序的结果显示了该复选框,但不允许我在该复选框上打勾: 在此处输入图片说明

public class ETableModel extends AbstractTableModel {

    private boolean checked;

    private List<Employee> eDb;

    private String[] colNames = {"Select", "Name", "National Insurance Number", "National Insurance Catagory",
            "Contact number", "Email", "Address", "Town/City", "Postcode"};

    public String getColumnName(int column) {
        return colNames[column];
    }

    public ETableModel(){
    }

    public boolean isCellEdditable(int row, int col){
        switch(col){
        case 0:
            return true;
        default:
            return false;
        }
    }

    public void setData(List<Employee> eDB){
        this.eDb = eDB;
    }

    @Override
    public int getColumnCount() {
        return colNames.length;
    }

    @Override
    public int getRowCount() {
        return eDb.size();
    }

    @Override
    public Object getValueAt(int row, int col) {
        Employee employee = eDb.get(row);

        switch(col){
        case 0:
            return checked;
        case 1:
            return employee.getName();
        case 2:
            return employee.getnINumber();
        case 3:
            return employee.getnICatagory();
        case 4:
            return employee.getMobileNum();
        case 5:
            return employee.getEmail();
        case 6:
            return employee.getAddress();
        case 7:
            return employee.getArea();
        case 8:
            return employee.getPostCode();
        }
        return null;
    }

    @Override
    public void setValueAt(Object value, int row, int col){
        switch(col){
        case 0:
            checked = ((Boolean)value);
            fireTableCellUpdated(row, col);
        default:
            return;
        }
    }

    @Override
    public Class<?> getColumnClass(int col) {
        switch(col){
        case 0:
            return Boolean.class;
        case 1:
            return String.class;
        case 2:
            return String.class;
        case 3:
            return NICatagory.class;
        case 4:
            return Integer.class;
        case 5:
            return String.class;
        case 6:
            return String.class;
        case 7:
            return String.class;
        case 8:
            return String.class;
        default:
            return null;
        }
    }

}

Does anyone have any pointers? 有人有指针吗? I'm guessing the issue is within my setValueAt() method but I am not sure how to correct this. 我猜这个问题在我的setValueAt()方法中,但是我不确定如何解决这个问题。 Most example are using: 大多数示例正在使用:

Data[int row][int col] = ((Boolean) value); Data [int row] [int col] =((Boolean)value);

However, I am not sure how to use this when my values are being taken from user input. 但是,当从用户输入中获取我的值时,我不确定如何使用此功能。

I'm guessing the issue is within my setValueAt() method 我猜问题在我的setValueAt()方法中

Yes, in that method you need to update member eDB in class ETableModel . 是的,在这个方法需要更新成员eDBETableModel

Also, you spelled isCellEditable wrong. 另外,您将isCellEditable拼写错误。 Is this a typo? 这是错字吗? If it is not then you need to correct your code. 如果不是,那么您需要更正您的代码。

Good Luck! 祝好运!

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

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