Java 在抽象表模型中实现复选框

Java 在抽象表模型中实现复选框,java,swing,Java,Swing,我们正在尝试实现扩展抽象表模型的表模型。我们在抽象表模型中实现了复选框。到目前为止,我们无法更改复选框的值。请给我们一些建议 以下是FileTableModel的代码: import javax.swing.table.AbstractTableModel; import java.text.SimpleDateFormat; public class FileTableModel extends AbstractTableModel { private FileList sourc

我们正在尝试实现扩展抽象表模型的表模型。我们在抽象表模型中实现了复选框。到目前为止,我们无法更改复选框的值。请给我们一些建议

以下是FileTableModel的代码:

import javax.swing.table.AbstractTableModel;
import java.text.SimpleDateFormat;

public class FileTableModel extends AbstractTableModel {

    private FileList sourceList;
    private FileList targetList;
    private ActionList actionList;

    private final String[] COLUMN_NAME = {"Name(Source)", "Size(B)", "Last modified", "Action",
        "Name(Target)", "Size(B)", "Last modified"};

    private Class[] columnClasses = new Class[]{
        String.class, Long.class, SimpleDateFormat.class, String.class,
        String.class, Long.class, SimpleDateFormat.class,
    };

    public FileTableModel(FileList sourceList, FileList targetList, ActionList actionList) {
        this.sourceList = sourceList;
        this.targetList = targetList;
        this.actionList = actionList;
    }

    public int getColumnCount() {
        return 7;
    }  // A constant for this model


    public int getRowCount() {
        return targetList.size();
    }  // # of files in dir


    public String getColumnName(int col) {
        return COLUMN_NAME[col];
    }


    public Class getColumnClass(int col) {
        return columnClasses[col];
    }

    public Object getValueAt(int row, int col) {
        switch (col) {
            case 0:
                if (sourceList.get(row) != null) return sourceList.get(row).getName();
                else return "";
            case 1:
                if (sourceList.get(row) != null) {
                    if (!sourceList.get(row).isDirectory())
                        return new Long((sourceList.get(row).length()));
                    else return "<Folder>";
                } else return "";

            case 2:
                SimpleDateFormat sourceDate = new SimpleDateFormat("HH:mm:ss");
                if (sourceList.get(row) != null)
                    return sourceDate.format(sourceList.get(row).lastModified());
                else return "";

            case 3:
                if (actionList.get(row) != null)
                    return actionList.get(row);
                else return "";
            case 4:
                if (targetList.get(row) != null) return targetList.get(row).getName();
                else return "";
            case 5:
                if (targetList.get(row) != null) {
                    if (!targetList.get(row).isDirectory())
                        return new Long((targetList.get(row).length()));
                    else return "<Folder>";
                } else return "";
            case 6:
                SimpleDateFormat targetDate = new SimpleDateFormat("HH:mm:ss");
                if (targetList.get(row) != null)
                    return targetDate.format(targetList.get(row).lastModified());
                else return "";

            default:
                return null;
        }
    }
}
import javax.swing.table.AbstractTableModel;
导入java.text.simpleDataFormat;
公共类FileTableModel扩展了AbstractTableModel{
私有文件列表源列表;
私有文件列表目标列表;
私人行动清单;
private final String[]COLUMN_NAME={“NAME(Source)”,“Size(B)”,“Last modified”,“Action”,
“名称(目标)”,“大小(B)”,“上次修改”};
私有类[]列类=新类[]{
String.class、Long.class、SimpleDataFormat.class、String.class、,
String.class、Long.class、simpleDataFormat.class、,
};
公共FileTableModel(FileList sourceList、FileList targetList、ActionList ActionList){
this.sourceList=sourceList;
this.targetList=targetList;
this.actionList=actionList;
}
public int getColumnCount(){
返回7;
}//此模型的常量
public int getRowCount(){
返回targetList.size();
}目录中的文件的//#
公共字符串getColumnName(int-col){
返回列名称[col];
}
公共类getColumnClass(int-col){
返回列类[col];
}
公共对象getValueAt(整数行,整数列){
开关(col){
案例0:
if(sourceList.get(row)!=null)返回sourceList.get(row.getName();
否则返回“”;
案例1:
if(sourceList.get(row)!=null){
如果(!sourceList.get(row.isDirectory())
返回新的Long((sourceList.get(row.length());
否则返回“”;
}否则返回“”;
案例2:
SimpleDataFormat sourceDate=新的SimpleDataFormat(“HH:mm:ss”);
if(sourceList.get(row)!=null)
返回sourceDate.format(sourceList.get(row.lastModified());
否则返回“”;
案例3:
if(actionList.get(row)!=null)
返回actionList.get(行);
否则返回“”;
案例4:
if(targetList.get(row)!=null)返回targetList.get(row).getName();
否则返回“”;
案例5:
if(targetList.get(行)!=null){
如果(!targetList.get(row.isDirectory())
返回新的Long((targetList.get(row.length());
否则返回“”;
}否则返回“”;
案例6:
SimpleDataFormat targetDate=新的SimpleDataFormat(“HH:mm:ss”);
if(targetList.get(行)!=null)
返回targetDate.format(targetList.get(row.lastModified());
否则返回“”;
违约:
返回null;
}
}
}

您的表格模型当前是只读的。添加此方法以使其可编辑:

@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
    return true;
}
private static final int COLUMN_INDEX_CHECK_BOX = 28;

@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
    return columnIndex == COLUMN_INDEX_CHECK_BOX;
}
也可以仅使特定列可编辑:

@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
    return true;
}
private static final int COLUMN_INDEX_CHECK_BOX = 28;

@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
    return columnIndex == COLUMN_INDEX_CHECK_BOX;
}
//包用户接口;
//导入core.ActionList;
//导入core.FileList;
导入javax.swing.table.AbstractTableModel;
导入java.text.simpleDataFormat;
公共类FileTableModel扩展了AbstractTableModel{
私有文件列表源列表;
私有文件列表目标列表;
私人行动清单;
私人检查表;
private final String[]COLUMN_NAME={“NAME(Source)”,“Size(B)”,“Last modified”,“Action”,
“名称(目标)”、“大小(B)”、“上次修改”、“启用操作”};
私有类[]列类=新类[]{
String.class、Long.class、SimpleDataFormat.class、String.class、,
String.class、Long.class、SimpleDataFormat.class、Boolean.class
};
/**
*类FileTableModel的构造函数
*用法:
*
*@param:
*/
公共FileTableModel(FileList sourceList、FileList targetList、ActionList ActionList){
this.sourceList=sourceList;
this.targetList=targetList;
this.actionList=actionList;
this.checkList=新布尔值[1000];
//将检查表的所有值初始化为false
对于(int i=0;i<1000;i++){
该检查表[i]=正确;
}
}
public void refreshttablemodel(文件列表源列表、文件列表目标列表、操作列表操作列表){
sourceList.clear();
targetList.clear();
actionList.clear();
this.sourceList=sourceList;
this.targetList=targetList;
this.actionList=actionList;
对于(int i=0;i<1000;i++){
该检查表[i]=正确;
}
this.fireTableDataChanged();
}
/**
*@返回
*/
public int getColumnCount(){
返回8;
}//此模型的常量
/**
*@返回
*/
public int getRowCount(){
返回targetList.size();
}目录中的文件的//#
/**
*@param col
*@返回
*/
公共字符串getColumnName(int-col){
返回列名称[col];
}
/**
*@param col
*@返回
*/
公共类getColumnClass(int-col){
返回列类[col];
}
/**
*@param行
*@param col
*@返回
*/
@凌驾
公共布尔值可编辑(int行,int列){
如果(列==7)
返回true;
否则返回false;
}
@凌驾
public void setValueAt(对象有效、int行索引、int列索引){
如果(检查表[行索引])
检查表[rowIndex]=错误;
else检查表[rowIndex]=真;
fireTableCellUpdated(行索引、列索引);
}
公共对象getValueAt(整数行,整数列){
开关(col){
案例0: