Can anybody please check this code out?

L

Lorenzo

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.
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???

Thank you ahead


lor
 
C

Chris Dollin

Lorenzo said:
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.
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.

I can't answer your actual question, alas, but:
public boolean isCellEditable( int rowIndex , int columnIndex)
{
if(rowIndex > columnIndex)
{
return false;
}
else
{
return true;
}
}

Wouldn't `return rowIndex <= columnIndex;` be more straightforward?
 
A

ali

well i am not sure i have understood your question but there is
something fishy
public class UD_JTable extends JTable
{
public UD_JTable(int rows, int cols)
{
super();
}


you are not saving the number of rows nor the number of cols in your
class neither you are passing it to the super class i think you should
pass it to super class

by the way where is the exact line of code that do generate the error
 
P

Patricia Shanahan

Lorenzo said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top