gpt4 book ai didi

java - JTable、isCellEditable 返回 true 并且单元格不可编辑

转载 作者:行者123 更新时间:2023-12-01 22:50:56 25 4
gpt4 key购买 nike

我在使用 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);
}
}

最佳答案

问题似乎出在这里:

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 block 都不将值 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 为 false,然后您将获得 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
}

关于java - JTable、isCellEditable 返回 true 并且单元格不可编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24741240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com