extends jtable

6

6e

I made a class that extends jtable. But its acting a bit weird.

The first thing I do in the constructor is to call the
super(data,columnnames). This would seemingly make a Jtable object
yes?

However each time that I try to say... this.getColumnCount(), the
result is 0. if I try to get a specific column, of course I get a
arrayindexoutofbounds -1 error.

Im confused and lost as to what the problem is...

Ive posted some simplified code below for your perusal...

Thanks!

-----------"main" code--------------------------------

String[] columnNames = {"Format Label", "Role", "Name", "", "", "", "",
""};
Object[][] data = {
{" ", " ", " ", imgSpecial, imgUp, imgDown, imgToTop, imgToBottom},
{" ", " ", " ", imgSpecial, imgUp, imgDown, imgToTop, imgToBottom}
};

myTable = new MyTable(data, columnNames);

----MyTable constructor...------------------------------------

public MyTable(Object[][] data, String[] columnNames){

super(data,columnNames);


//set font size
this.setFont(new Font("Courier", Font.PLAIN, 13));

//no column moving
this.getTableHeader().setReorderingAllowed(false);
System.out.println("Hltable column count =" + this.getColumnCount());
//equals zero!
this.getColumnModel().getColumn(this.convertColumnIndexToView(iRole));
//breaks the code with an arrayindexoutofbounds error..
 
J

jan V

I made a class that extends jtable. But its acting a bit weird.

Extending most classes requires knowledge of the superclass' implementation.
JTable's complexity and API design are telltale signals that the JTable
authors intended the class to be instantiated as-is, not subclassed. Of
course, you can subclass it, but then you better know what you're doing; and
since JTable is quite complex, subclassing it would have to be justified
with some strong arguments. Why are you subclassing it?
The first thing I do in the constructor is to call the
super(data,columnnames). This would seemingly make a Jtable object
yes?

In practice, real-life JTables delegate all data storage/management to
TableModels... so that constructor would not be used in a real-life program.
(By the way, when giving constructor signatures, you need to use the
argument TYPE names, not the names of the formal arguments..)
any ideas? Thanks again!

Create your own TableModel. It's a far more powerful technique, and once
you've done it once, you'll never feed a JTable anything else but custom
TableModels...
 
V

Vova Reznik

6e said:
I made a class that extends jtable. But its acting a bit weird.

The first thing I do in the constructor is to call the
super(data,columnnames). This would seemingly make a Jtable object
yes?

However each time that I try to say... this.getColumnCount(), the
result is 0. if I try to get a specific column, of course I get a
arrayindexoutofbounds -1 error.

this.getColumnModel().getColumn(this.convertColumnIndexToView(iRole));

form doc JTable::convertColumnIndexToView(int)
returns -1 if this column is not being displayed
IS NOT BEING DISPLAYED
 
6

6e

Im extending the class to allow for customized highlighting effects, as
well as creating a standard table design associated with custom
cellrenderers and editors.

When calling the super(data,columnnames). i am calling it with the
correct variable types. The variable types as specified within the
code is Object[][] data , String[] columnNames .

Extending JTable does not mean that I need to reprogram all of the
JTable's functions, only the ones I want to override, like the
constructor, so I dont believe that it is extremely complex. There are
plenty of samples on the web that do extend jtable, so I am not wary of
extending it.
 
6

6e

Vova - Thanks! I didnt know that. im going to look into it, because
that may be the issue.
 
V

Vova Reznik

6e said:
didnt seem to fix my problem...
That else did you brake (override)?
What about TableModel you use (responsible for column count)?
Why do you use
this.convertColumnIndexToView(iRole)
in constructor?
Why do you use
this.convertColumnIndexToView(iRole)
at all if you set reordering false?

I have another million questions.

But main thing - your model returns 0 as column count.
Find the bug
 
6

6e

Even when I comment out that line of code, the next time that I refer
to the table I get an arrayindexoutofbounds error of -1

for instance...
myTable.getColumnModel().getColumn(myTable.convertColumnIndexToView(1)).setCellRenderer(new
IconCellRenderer());
located after scrollpane.setViewport(myTable);

the model returning 0 is part of my mass of confusion. The constructor
should make the table, I send it the proper array and matrix that
should create a successful table, yet why is the ColumnCount = 0?

Perhaps I should scrap it and work forwards rather than searching for
the bug, Ive been working on this bug for 2 days...
 
J

jan V

Perhaps I should scrap it and work forwards rather than searching for
the bug, Ive been working on this bug for 2 days...

What about using a single-stepping debugger? Should take you 20 mins at most
to find the source of the problem. Are you using Eclipse, or Netbeans or
IntelliJ ? What's your development environment?
 
J

jan V

eclipse...

Well.... how about setting a breakpoint in your constructor... and stepping
through the code statement by statement? ;-)
 
6

6e

I did! :)

But it doesn't really reveal much. The only thing that seems weird is
that the data object is showing up as Object[][2]... but I THINK Im
creating the object correctly. I created a tiny program to just run
through the constructor, but it still seems like its not working...

I mean AM I creating the Object data incorrectly? When I look at other
code, it seems to be correct.... any ideas?

main(){
String[] columnNames = {"Format Label", "Role", "Name", "", "", "", "",
""};
Object[][] data = {
{" ", " ", " ", "", "", "", "", ""},
{" ", " ", " ", "", "", "", "", ""}
};
HighlightTable myTable = new HighlightTable(data,
columnNames,0,1,2,3,4,5,6,7);
}
-----------------------
public HighlightTable(Object[][] data, String[] columnNames, int
iFORMAT, int iNAME, int iROLE,
int iCOLUP, int iCOLDOWN, int iCOLTOTOP, int iCOLTOBOTTOM,
int iSPECIAL){

super(data,columnNames);

iColUp = iCOLUP;
iColDown = iCOLDOWN;
iColToTop = iCOLTOTOP;
iColToBottom = iCOLTOBOTTOM;
iSpecial = iSPECIAL;
iFormat = iFORMAT;
iRole = iROLE;
iName = iNAME;

//set font size
this.setFont(new Font("Courier", Font.PLAIN, 13));

//no column moving
//adam test
this.getTableHeader().setReorderingAllowed(false);
this.getColumnModel().getColumn(this.convertColumnIndexToView(iRole));
}
}
 
V

Vova Reznik

6e said:

Surpise :)
Did you find why it works now?

I mean AM I creating the Object data incorrectly? When I look at other
code, it seems to be correct.... any ideas?
That piece is correct.

this.getTableHeader().setReorderingAllowed(false);
this.getColumnModel().getColumn(this.convertColumnIndexToView(iRole));

If reordering is false, then no reason to use convertColumnIndexToView(int)
 
R

Roedy Green

any ideas? Thanks again!

In general, you can't rely on querying GUI objects in the constructor.
They are not fully set up until they have gone through several
processes:

adding to a container
validate of enclosing container
setVisible of enclosing container
addNotify

Best to query them later on. One useful hook place is addNotify.

see http://mindprod.com/jgloss/addnotify.html

In your particular case, you can get the info you want from
data[0].length.
 
R

Roedy Green

you can subclass it, but then you better know what you're doing; and
since JTable is quite complex, subclassing it would have to be justified
with some strong arguments. Why are you subclassing it?

Normally what you would do is subclass something simpler like JFrame
and include a JTable component in it.
 
R

Roedy Green

In practice, real-life JTables delegate all data storage/management to
TableModels... so that constructor would not be used in a real-life program.
(By the way, when giving constructor signatures, you need to use the
argument TYPE names, not the names of the formal arguments..)

I saw that super(data, columnNames) constructor and said to myself
"what the F??". I looked it up and indeed JTable has a simple version
without a TableModel. You just feed it your data directly. Presumably
it internally generates a minimalist read-only TableModel for you.
 
J

jan V

In practice, real-life JTables delegate all data storage/management to
I saw that super(data, columnNames) constructor and said to myself
"what the F??". I looked it up and indeed JTable has a simple version
without a TableModel. You just feed it your data directly. Presumably
it internally generates a minimalist read-only TableModel for you.

It does. I feel a bit guilty here... when Swing first came out (remember the
com.sun.swing nonsense?) I actually criticised Sun for making so much of
Swing so complex. I explicitly mentioned JTable, pointing at the complexitiy
of the constructors... so they added some simpler ones. It's only later I
realized Sun's original approach was right, and my suggestion was a
simplification too far (I'm no Einstein ;-). The OP's use of this
constructor is proof of that.
 
J

jan V

if you overrode any of JTable's methods, you had better call
super.xxxx() somewhere in your method.

That's the problem with subclassing really complex classes like JTable, you
almost have to have the same knowledge as the parent class' author to be
able to safely create non-trivial extensions.

Including super.xxxx() is often a requirement, but not always. And when it
is, you may need to put your super call in a very specific place in your
overridden method (first statement, last statement, or somewhere in the
middle?).

That's why I advocate thinking really hard before one subclasses mega
classes like JTable. Its API clearly provides a whole spectrum of hooks to
customize its behaviour (renderers, data model, selection model, etc..
etc..), so people should try extra-hard to see if their requirements can be
satisfied by leveraging those hook mechanisms without any subclassing.
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top