Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

extends jtable

2 views
Skip to first unread message

6e

unread,
Sep 2, 2005, 10:28:17 AM9/2/05
to
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..

--------------

any ideas? Thanks again!

6e

unread,
Sep 2, 2005, 10:35:47 AM9/2/05
to
where iRole = 1...

sorry forgot to include that....

jan V

unread,
Sep 2, 2005, 10:40:11 AM9/2/05
to
> 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...


Vova Reznik

unread,
Sep 2, 2005, 10:49:36 AM9/2/05
to
6e wrote:
> 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

6e

unread,
Sep 2, 2005, 10:50:32 AM9/2/05
to
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.

6e

unread,
Sep 2, 2005, 10:51:55 AM9/2/05
to
Vova - Thanks! I didnt know that. im going to look into it, because
that may be the issue.

6e

unread,
Sep 2, 2005, 11:09:29 AM9/2/05
to
didnt seem to fix my problem...

Vova Reznik

unread,
Sep 2, 2005, 11:23:29 AM9/2/05
to
6e wrote:
> 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

6e

unread,
Sep 2, 2005, 11:36:49 AM9/2/05
to
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...

jan V

unread,
Sep 2, 2005, 2:08:20 PM9/2/05
to
> 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?


6e

unread,
Sep 2, 2005, 2:15:31 PM9/2/05
to
eclipse...

jan V

unread,
Sep 2, 2005, 2:36:14 PM9/2/05
to
> eclipse...

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


6e

unread,
Sep 2, 2005, 3:14:30 PM9/2/05
to
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));
}
}

Vova Reznik

unread,
Sep 2, 2005, 3:49:17 PM9/2/05
to
6e wrote:
> I did! :)

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)

Roedy Green

unread,
Sep 2, 2005, 7:23:51 PM9/2/05
to
On 2 Sep 2005 07:28:17 -0700, "6e" <aot...@hotmail.com> wrote or
quoted :

>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.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Roedy Green

unread,
Sep 2, 2005, 7:25:01 PM9/2/05
to
On Fri, 02 Sep 2005 14:40:11 GMT, "jan V" <n...@nul.be> wrote or quoted
:

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

Roedy Green

unread,
Sep 2, 2005, 7:27:44 PM9/2/05
to
On Fri, 02 Sep 2005 14:40:11 GMT, "jan V" <n...@nul.be> wrote or quoted
:

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

Roedy Green

unread,
Sep 2, 2005, 10:10:14 PM9/2/05
to
On 2 Sep 2005 07:28:17 -0700, "6e" <aot...@hotmail.com> wrote or
quoted :

>any ideas? Thanks again!

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

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

jan V

unread,
Sep 3, 2005, 4:11:26 AM9/3/05
to
> >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.

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.


jan V

unread,
Sep 3, 2005, 4:17:58 AM9/3/05
to
> 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.


cym tronik

unread,
Sep 6, 2005, 5:33:05 PM9/6/05
to

I often subclass JTables, exactely in the way you do. I do so because
I need some additional information beeing bundled with the table. I
have no problems with them.

here is my working code, taken from a application that deals with 30 M
CHF per year, so you imagine it was tested thoroughly:

class _Table extends JTable
{
private String[] _aSet;

// CONSTRUCTOR ...................................................................
public _Table(String[][] values, Object[] header, String[] a) {
super(values, header);
_aSet = (String[])a.clone();
} // constructor

// getTexts ----------------------------------------------------------------------
public String[] getTexts() {
return _aSet;
} // getTexts

} // class _Table

the Java version is j2sdk1.4.2_04

Cymtron

6e

unread,
Sep 7, 2005, 3:27:52 PM9/7/05
to
I fixed it! Thanks to your example, I decided to write a new table
class, and slowly add the code from my previous works...

Basically when I overrode (tableChanged), it caused my table to
literally throw up all of its data and blank out on me. EVEN WHEN IT
WAS A BLANK FUNCTION!

Man that pissed me off.

Thanks for the help everyone!

Andrew Thompson

unread,
Sep 7, 2005, 3:33:35 PM9/7/05
to
On 7 Sep 2005 12:27:52 -0700, 6e wrote:

> Basically when I overrode (tableChanged), it caused my table to
> literally throw up all of its data and blank out on me. EVEN WHEN IT
> WAS A BLANK FUNCTION!

I find that completely unastounding, did you do any
tracing to find what was calling it? What about when
you overode it to simply call 'super()'?

--
Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
"How can all these questions ..keep f*ckin' with my head?"
Spearhead 'Hole in the Bucket'

0 new messages