Re: Can anybody please check this code out?
Lorenzo wrote:
Hi all,
I'm rather inexperienced in Java but I'm in need of defining a subclass
of JTable(N,N) with non-editable underdiagonal elements. I do not know
if it's better to do it by using TableModel or directly JTable, but I
used the latter because it looked easier to me.
Can the user rearrange the columns in your table? If so, imposing this
condition on the model and on the table will have different effects, and
probably only one of them does the right thing.
The following code gives no errors in NetBeans, but using the newborne
UD_JTable class in MATLAB I get a very nasty error which pretty much
looks like some sort of memory allocation fault.
Here is the code:
import javax.swing.*;
import javax.swing.table.*;
public class UD_JTable extends JTable
{
public UD_JTable(int rows, int cols)
{
super();
}
public boolean isCellEditable( int rowIndex , int columnIndex)
{
if(rowIndex > columnIndex)
{
return false;
}
else
{
return true;
}
}
public static void main(String[] args)
{
}
}
And here is the error
??? Java exception occurred:
java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.util.Vector.elementAt(Unknown Source)
at javax.swing.table.DefaultTableColumnModel.getColumn(Unknown Source)
at javax.swing.JTable.convertColumnIndexToModel(Unknown Source)
at javax.swing.JTable.setValueAt(Unknown Source)
.
Does anybody have a clue of what's happening???
I don't know enough about JTable to answer your specific question, but
have a general comment about this sort of overriding. Your problem
description contains no reason to make a cell editable that would not be
editable in the superclass. In that situation, I would do:
public boolean isCellEditable(int rowIndex, int columnIndex)
{
return rowIndex <= columnIndex &&
super.isCellEditable(rowIndex, columIndex);
}
so that a cell must be editable according to the superclass, as well as
being on or above the main diagonal, to be considered editable by the
subclass implementation.
Patricia