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

标签 java swing

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

这是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;
        }
    }
}

最佳答案

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

@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;
}

关于java - 在抽象表模型中实现复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34267691/

相关文章:

java - JUNIT Autowired 实例始终为 null

java - OnClickListener 只执行一次

java - 是否有返回 true、false 或 unknown 的 Java 约定?

java - JPanel 调整大小显示另一个 JPanel

java - 如果 JTextFeild 在 JFrame 中,则 JTextPane select() 不起作用

java - 如何更新 JTable 中 DefaultTableModel 的行?

java - Spring MVC Controller 映射不起作用

java - 如何创建一个JComboBox,其每个元素都有两种不同的样式?

java - 将 Graphics2D 绘制到另一个 Graphics2D

java - Swing - 来自 Color 的新 ImageIcon