user3419487
user3419487

Reputation: 185

Implementing check box in abstract table model

We are trying implement the table model which extends abstract table model. We have implemented the check box in the abstract table model. Until now we are not able to change the value of the check box. Please give us some suggestions.

Here is the code of 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;
        }
    }
}

Upvotes: 1

Views: 229

Answers (2)

user3419487
user3419487

Reputation: 185

//package userinterface;

//import core.ActionList;
//import core.FileList;

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

public class FileTableModel extends AbstractTableModel {

    private FileList sourceList;
    private FileList targetList;
    private ActionList actionList;
    private Boolean[] checkList;

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

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

    /**
     * Constructor for class FileTableModel
     * Usage:
     *
     * @param:
     */
    public FileTableModel(FileList sourceList, FileList targetList, ActionList actionList) {
        this.sourceList = sourceList;
        this.targetList = targetList;
        this.actionList = actionList;
        this.checkList = new Boolean[1000];
        // Initialize all the values of checkList to false
        for (int i = 0; i < 1000; i++) {
            this.checkList[i] = true;
        }
    }

    public void refreshTableModel(FileList sourceList, FileList targetList, ActionList actionList) {
        sourceList.clear();
        targetList.clear();
        actionList.clear();
        this.sourceList = sourceList;
        this.targetList = targetList;
        this.actionList = actionList;
        for (int i = 0; i < 1000; i++) {
            this.checkList[i] = true;
        }
        this.fireTableDataChanged();
    }

    /**
     * @return
     */
    public int getColumnCount() {
        return 8;
    }  // A constant for this model

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

    /**
     * @param col
     * @return
     */
    public String getColumnName(int col) {
        return COLUMN_NAME[col];
    }

    /**
     * @param col
     * @return
     */
    public Class<?> getColumnClass(int col) {
        return columnClasses[col];
    }

    /**
     * @param row
     * @param col
     * @return
     */

    @Override
    public boolean isCellEditable(int row, int col) {
        if (col == 7)
            return true;
        else return false;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        if(checkList[rowIndex])
            checkList[rowIndex] = false;
        else checkList[rowIndex] = true;
        fireTableCellUpdated(rowIndex, columnIndex);
    }

    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 "";
            case 7:
                return checkList[row];

            default:
                return null;
        }
    }



}

Upvotes: 0

Freek de Bruijn
Freek de Bruijn

Reputation: 3622

Your table model is currently read-only. Add this method to make it editable:

@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
    return true;
}

You can also make only specific columns editable:

private static final int COLUMN_INDEX_CHECK_BOX = 28;

@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
    return columnIndex == COLUMN_INDEX_CHECK_BOX;
}

Upvotes: 2

Related Questions