i have a problem with JTable
and isCellEditable
. I use two arrays to make a cell editable:
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];
}
}
but when i try edit last cell, method return true and cell isn't editable. Why?
Update
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);
}
This is my TableModel Class
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);
}
}