如何设置JTable中的JCheckbox可编辑?
How to set JCheckbox in JTable edditable?
我有 JTable 用于显示用户输入的值。使用 class 'Employee' 我从用户那里获取值并将它们显示在 table 中。我使用了 getColumnClass()、setValueAt() 和 isCellEdditable 方法。我程序的结果显示了复选框,但不允许我勾选该框:
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;
}
}
}
有人指点一下吗?我猜问题出在我的 setValueAt() 方法中,但我不确定如何更正此问题。大多数示例都在使用:
Data[int row][int col] = ((Boolean) value);
但是,当我的值是从用户输入中获取时,我不确定如何使用它。
I'm guessing the issue is within my setValueAt() method
是的,您需要在 class ETableModel
.
中更新成员 eDB
此外,您 isCellEditable
拼写错误。这是一个错字吗?如果不是,那么您需要更正您的代码。
祝你好运!
我有 JTable 用于显示用户输入的值。使用 class 'Employee' 我从用户那里获取值并将它们显示在 table 中。我使用了 getColumnClass()、setValueAt() 和 isCellEdditable 方法。我程序的结果显示了复选框,但不允许我勾选该框:
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;
}
}
}
有人指点一下吗?我猜问题出在我的 setValueAt() 方法中,但我不确定如何更正此问题。大多数示例都在使用:
Data[int row][int col] = ((Boolean) value);
但是,当我的值是从用户输入中获取时,我不确定如何使用它。
I'm guessing the issue is within my setValueAt() method
是的,您需要在 class ETableModel
.
eDB
此外,您 isCellEditable
拼写错误。这是一个错字吗?如果不是,那么您需要更正您的代码。
祝你好运!