JTable,isCellEditable返回true,单元格不可编辑

时间:2014-07-14 16:26:28

标签: java swing jtable abstracttablemodel

我对JTableisCellEditable有疑问。我使用两个数组来使单元格可编辑:

private boolean cEFilm[]={true,true,true,true,false,false,false,false,true,false};
private boolean cETv[]={true,true,true,false,true,true,true,true,true,true,true};

public boolean isCellEditable(int row, int col) 
{
    if(getValueAt(row,0) instanceof SerieTv) // first column contain a SerieTv object 
    {
        //System.out.println("TV="+cETv[col]);
        return cETv[col];
    }
    else 
    {
        //System.out.println("Film="+cEFilm[col]);
        return cEFilm[col];
    }
}

但是当我尝试编辑最后一个单元格时,方法返回true并且单元格不可编辑。为什么呢?

更新

public class PanelTF extends JPanel implements Serializable, ActionListener, MouseListener
{
private JPanel pan_4 = new JPanel();
private JPanel pan_g = new JPanel();    
private TableModel tModel;
private JTable table;
private JScrollPane JSPTTable;

public PanelTF()
{
/* ... */
tModel=new TableModel(sinTv.getListTv());
table=new JTable(tModel) {
        public Dimension getPreferredScrollableViewportSize() {
            return new Dimension(500, 200);
        }
    };
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);

for (int i=0; i<table.getColumnCount(); i++)
{
TableColumn column = table.getColumnModel().getColumn(i);
}

table.setBackground(Color.MAGENTA);
table.setFillsViewportHeight(true);
JSPTTable = new JScrollPane(table);
pan_4.setBackground(Color.yellow);
pan_4.add(JSPTTable);
pan_g.setLayout(new BoxLayout(pan_g,BoxLayout.Y_AXIS));
pan_g.add(pan_4);
}

这是我的TableModel类

import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
public class TableModel extends AbstractTableModel 
{
    private ArrayList<IFS> listaMista;

    public TableModel(ArrayList<IFS> listaMista) 
    {
        this.listaMista = listaMista;
    }

    public int getRowCount() 
    {
        return listaMista.size();
    }

    public int getColumnCount() 
    {
        return 9;
    }

    public String getColumnName(int column) 
    {
        switch (column) 
        {
            case 0: return "Tipo";
            case 1: return "Titolo";
            case 2: return "Alias";
            case 3: return "Regista";
            case 4: return "Num. Ep";
            case 5: return "Ep1";
            case 6: return "Ep2";
            case 7: return "Ep3";
            case 8: return "A. inizio";
            case 9: return "A. fine";           
        }
        return "";
    }

    public Class getColumnClass(int column) 
    {
        switch (column) 
        {
            case 0: return IFS.class;   // tipo
            case 1: return String.class;    // titolo
            case 2: return String.class;    // alias
            case 3: return String.class;    // regista  
            case 4: return Number.class;    // numEp
            case 5: return String.class;    // ep1  
            case 6: return String.class;    // ep2
            case 7: return String.class;    // ep3
            case 8: return Number.class;    // anno inizio 
            case 9: return Number.class;    // anno fine 
        }
        return Object.class;
    }

    public boolean isCellEditable(int row, int column) 
    {
        return true;
    }

    public Object getValueAt(int row, int column)
    {
        IFS ifs = listaMista.get(row);
        if(ifs instanceof SerieTv)
        {
            SerieTv serie=(SerieTv) ifs;
            switch (column) 
            {
                case 1: return serie.getTitolo();
                case 2: return serie.getAlias();
                case 3: break;  // auto-boxing!
                case 4: return serie.getNumEp();    // auto-boxing!
                case 5: return serie.getEp1();  // auto-boxing!
                case 6: return serie.getEp2();  // auto-boxing!
                case 7: return serie.getEp3();  // auto-boxing!
                case 8: return serie.getAnno(); // auto-boxing!
                case 9: return serie.getAnnoFine(); // auto-boxing!
            }
        }
        if(ifs instanceof FilmTv)
        {
            FilmTv filmtv=(FilmTv) ifs;
            switch (column)
            {
                case 1: return filmtv.getTitolo();
                case 2: return filmtv.getAlias();
                case 3: return filmtv.getRegista(); 
                case 8: return filmtv.getAnno();
            }
        }
        if(ifs instanceof Film)
        {
            Film film=(Film) ifs;
            switch (column)
            {
                case 1: return film.getTitolo();
                case 2: return film.getAlias();
                case 3: return film.getRegista();   
                case 8: return film.getAnno();
            }
        }
        return null;
    }

    public void setValueAt(Object value, int row, int column) 
    {
        IFS ifs = listaMista.get(row);
        if(ifs instanceof SerieTv)
        {
            SerieTv serie=(SerieTv) ifs;
            switch (column) 
            {
                case 1: serie.setTitolo((String) value); break;
                case 2: serie.setAlias((String) value); break;
                case 3: break;
                case 4: serie.setNumEp((Integer) value); break;
                case 5: serie.setEp1((String) value); break;
                case 6: serie.setEp2((String) value); break;
                case 7: serie.setEp3((String) value); break;
                case 8: serie.setAnno((Integer) value); break;
                case 9: serie.setAnnoFine((Integer) value); break;
            }
        }
        if(ifs instanceof FilmTv)
        {
            FilmTv filmtv=(FilmTv) ifs;
            switch (column) 
            {
                case 1: filmtv.setTitolo((String) value); break;
                case 2: filmtv.setAlias((String) value); break;
                case 3: filmtv.setRegista((String) value); break;
                case 8: filmtv.setAnno((Integer) value); break;
            }
        }
        if(ifs instanceof Film)
        {
            Film film=(Film) ifs;
            switch (column) 
            {
                case 1: film.setTitolo((String) value); break;
                case 2: film.setAlias((String) value); break;
                case 3: film.setRegista((String) value); break;
                case 8: film.setAnno((Integer) value); break;
            }
        }
    }

    public void aggiungi(IFS ifs) 
    {
        listaMista.add(ifs);
        int row = listaMista.size() - 1;
        fireTableRowsInserted(row, row); 
    }
}

1 个答案:

答案 0 :(得分:3)

问题似乎在这里:

public boolean isCellEditable(int row, int col) {
    if(getValueAt(row,0) instanceof SerieTv) { // here is the problem
        // what is in here doesn't matter        
    } else {
        // what is in here doesn't matter either
    }
}

仔细查看getValueAt(rowIndex, columnIndex)实现,这将返回第一列的null:第一个,第二个或第三个if块都不将值0视为列索引:

public Object getValueAt(int row, int column) {
    IFS ifs = listaMista.get(row);
    if(ifs instanceof SerieTv) { // first if block
        SerieTv serie=(SerieTv) ifs;
        switch (column) {
            // missing case 0 here
            case 1: return serie.getTitolo();
            ...
        }
    }
    if(ifs instanceof FilmTv) { // second if block
        FilmTv filmtv=(FilmTv) ifs;
        switch (column) { 
            // missing case 0 here too
            case 1: return filmtv.getTitolo();
            ...
        }
    }
    if(ifs instanceof Film) { // third if block
        Film film=(Film) ifs;
        switch (column) {
            // missing case 0 here too
            case 1: return film.getTitolo();
            ...
        }
    }
    return  null; // this is what you get on getValueAt(rowIndex, 0)
}

说完这一切后,您的isCellEditable(rowIndex, columnIndex)实施会返回false,因为null instanceof SerieTv为假,然后您获得了cEFilm的第10个元素,这确实是,false

public boolean isCellEditable(int row, int col) {
    if(getValueAt(row,0) instanceof SerieTv) { // getValueAt(row, 0) == null        
        return cETv[col];
    }
    else {        
        return cEFilm[col]; // col == 9 == 10th element in cEFilm array == false
    }
}

建议的解决方案

根据你的评论:

public boolean isCellEditable(int row, int col) {
    if(getValueAt(row,0) instanceof SerieTv) // first column contain a SerieTv object
    ...
}

我认为一个选项可能就是这个:

public Object getValueAt(int row, int column) {
    IFS ifs = listaMista.get(row);
    if(column == 0) {
        return ifs;
    }
    // all your code here
}