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

Exception in Swing code using a JTable

1 view
Skip to first unread message

Wes Harrison

unread,
Oct 20, 2005, 9:12:52 AM10/20/05
to
I have a JTable that has its data loaded from a database. The program
executes the query and adds data to the model from its own thread and that's
about all it does at this stage. If I run the query 10 times then maybe
once I will get the following exception. It doesn't appear to be coming
from my code so I am wondering what is causing it and how I can resolve it.
It also doesn't seem to affect the operation of the program in any adverse
way. It goes away by itself and comes back by itself.

Exception in thread "AWT-EventQueue-0"
java.lang.ArrayIndexOutOfBoundsException: 1 >= 1
at java.util.Vector.elementAt(Vector.java:432)
at
javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.
java:280)
at
javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:1883)
at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1810)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java:142)
at javax.swing.JComponent.paintComponent(JComponent.java:742)
at javax.swing.JComponent.paint(JComponent.java:1005)
at
javax.swing.JComponent.paintWithOffscreenBuffer(JComponent.java:4963)
at javax.swing.JComponent.paintDoubleBuffered(JComponent.java:4916)
at javax.swing.JComponent._paintImmediately(JComponent.java:4859)
at javax.swing.JComponent.paintImmediately(JComponent.java:4666)
at
javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:451)
at
javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQu
eueUtilities.java:114)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.ja
va:242)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java
:163)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

I am using J2SE 5.0_05 on Windows Server 2003.

Thanks,

Wes


Thomas Weidenfeller

unread,
Oct 20, 2005, 9:49:06 AM10/20/05
to
Wes Harrison wrote:
> I have a JTable that has its data loaded from a database. The program
> executes the query and adds data to the model from its own thread and that's
> about all it does at this stage.

Own thread? Which could the the root of the problem. Please see the FAQ
for how to work with the Swing threading model.

> If I run the query 10 times then maybe
> once I will get the following exception.

Sounds very much like a threading problem.

> It doesn't appear to be coming
> from my code so I am wondering what is causing it and how I can resolve it.

It comes from your code - indirectly. The GUI system tries to repaint
parts of the table because of some event, but the table's column model
data is garbled.

/Thomas

--
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/

Vova Reznik

unread,
Oct 20, 2005, 9:51:29 AM10/20/05
to
Do you use EventQueue.invokeLater() to fill table?

Roedy Green

unread,
Oct 20, 2005, 7:33:44 PM10/20/05
to
On Thu, 20 Oct 2005 23:12:52 +1000, "Wes Harrison" <nos...@home.com>
wrote or quoted :

>Exception in thread "AWT-EventQueue-0"
>java.lang.ArrayIndexOutOfBoundsException: 1 >= 1
> at java.util.Vector.elementAt(Vector.java:432)
> at
>javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.
>java:280)

it sounds like Swing got the idea into its head that column x had to
be repainted. It then, in a paint method called your
TableModel.ColumnModel to get information about column x.
Unfortunately there was no column x in the Vector you it used to hold
the data.

Perhaps you had deleted it by then.
Perhaps it never existed and you lied to Swing in a fire.
Perhaps you deleted it and never bothered to inform Swing with a fire.
Perhaps you have muddled things by changing your JTable from multiple
threads.

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Wes Harrison

unread,
Oct 21, 2005, 9:58:45 AM10/21/05
to
"Thomas Weidenfeller" <nob...@ericsson.invalid> wrote in message
news:dj878j$o9s$1...@news.al.sw.ericsson.se...

> Wes Harrison wrote:
> > I have a JTable that has its data loaded from a database. The program
> > executes the query and adds data to the model from its own thread and
that's
> > about all it does at this stage.
>
> Own thread? Which could the the root of the problem. Please see the FAQ
> for how to work with the Swing threading model.
>
> > If I run the query 10 times then maybe
> > once I will get the following exception.
>
> Sounds very much like a threading problem.
>
> > It doesn't appear to be coming
> > from my code so I am wondering what is causing it and how I can resolve
it.
>
> It comes from your code - indirectly. The GUI system tries to repaint
> parts of the table because of some event, but the table's column model
> data is garbled.

I have read the FAQ and now I have moved all code which updates the table
model into the EDT using invokeLater() but it hasn't helped.

Wes


Wes Harrison

unread,
Oct 21, 2005, 9:59:23 AM10/21/05
to
"Vova Reznik" <byRe...@willTell.com> wrote in message
news:BhN5f.411$R%.252@newssvr23.news.prodigy.net...

> Do you use EventQueue.invokeLater() to fill table?

Yes, I do now. Same problem though.

Wes


Wes Harrison

unread,
Oct 21, 2005, 10:01:41 AM10/21/05
to
"Roedy Green" <my_email_is_post...@munged.invalid> wrote in
message news:au9gl1p0q1deifktv...@4ax.com...

> On Thu, 20 Oct 2005 23:12:52 +1000, "Wes Harrison" <nos...@home.com>
> wrote or quoted :
>
> >Exception in thread "AWT-EventQueue-0"
> >java.lang.ArrayIndexOutOfBoundsException: 1 >= 1
> > at java.util.Vector.elementAt(Vector.java:432)
> > at
>
>javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel
.
> >java:280)
>
> it sounds like Swing got the idea into its head that column x had to
> be repainted. It then, in a paint method called your
> TableModel.ColumnModel to get information about column x.
> Unfortunately there was no column x in the Vector you it used to hold
> the data.

I am not using a Vector to hold the data. I use an ArrayList.

> Perhaps you had deleted it by then.

There are no deletions.

> Perhaps it never existed and you lied to Swing in a fire.

Perhaps, but I have checked that part of the code and it looks fine.

> Perhaps you deleted it and never bothered to inform Swing with a fire.

There are no deletions.

> Perhaps you have muddled things by changing your JTable from multiple
> threads.

Not anymore, I use invokeLater().

Wes


Vova Reznik

unread,
Oct 21, 2005, 10:25:33 AM10/21/05
to
Do you use your own TableModel?
If yes - show it.

You (program) may change data structure (different number of columns for
example) with out notifying model about it.

Show example

Message has been deleted

Wes Harrison

unread,
Oct 21, 2005, 12:23:08 PM10/21/05
to
<jo...@vanderkogel.net> wrote in message
news:1129908612.2...@g43g2000cwa.googlegroups.com...

> > I am not using a Vector to hold the data. I use an ArrayList.
>
> > There are no deletions.

>
> > Perhaps, but I have checked that part of the code and it looks fine.
>
> > There are no deletions.

>
> > Not anymore, I use invokeLater().
>
> Well, that settles it, you're obviously not doing anything wrong. But
> still you're getting these errors, it's so strange. Really, there's
> only one logical reason left, the position of the stars is wrong for
> your kind of problem. The good news is that in a few millenia they will
> be properly alligned again.

Geez, what's up your nose? I only responded honestly to Roedy's comments in
what I thought was a civil and respectful manner. Obviously I am still
doing something wrong or I wouldn't be getting the exception. Take a few
Valium and lie down.

Wes


Wes Harrison

unread,
Oct 21, 2005, 12:29:44 PM10/21/05
to
"Vova Reznik" <byRe...@willTell.com> wrote in message
news:xT66f.575$R%.422@newssvr23.news.prodigy.net...

Here's the model I am using. DataRecord is a class containing the data
retrieved from the database.

class MyTableModel extends AbstractTableModel {

DataRecord dr;

public MyTableModel(DataRecord dr)
{
this.dr = dr;
}

public synchronized int getColumnCount() {
return this.dr.getColumnCount();
}

public synchronized int getRowCount() {
return dr.getRowCount();
}

public synchronized String getColumnName(int col) {
String columnName = "";
try {
columnName = this.dr.getColumnName(col);
} catch (SQLException ex) {
ex.printStackTrace();
}
return columnName;
}

public synchronized Object getValueAt(int row, int col) {
Object obj = dr.getValueAt(row, col);
if (obj == null)
{
return "<NULL>";
}
else
{
return obj;
}
}

public synchronized java.lang.Class getColumnClass(int c) {
if (getValueAt(0, c) == null)
return Object.class;
else
return getValueAt(0, c).getClass();
}
}

Wes


Vova Reznik

unread,
Oct 21, 2005, 12:53:20 PM10/21/05
to

DataRecord is not private here.
Where and how do you change data (DataRecord)?
Do you rebuild TableModel when new data arrived?

Why all methods are synchronized?

Wes Harrison

unread,
Oct 21, 2005, 1:02:30 PM10/21/05
to
"Vova Reznik" <byRe...@willTell.com> wrote in message
news:4296f.1010$Kv....@newssvr22.news.prodigy.net...

Is that an issue?

> Where and how do you change data (DataRecord)?

In a separate thread the data are loaded into DataRecord. The number of
rows is increasing all the time (until all rows are retrieved from the
database). Other than that there are no changes to the data. It's simply a
program that retrieves some data and displays it in a JTable.

> Do you rebuild TableModel when new data arrived?

Rebuild? I fire that rows have been inserted but that is all. What do you
mean by rebuild?

> Why all methods are synchronized?

I saw this in an example so I thought that these methods had to be
synchronized.

Thanks,

Wes


Vova Reznik

unread,
Oct 21, 2005, 2:09:09 PM10/21/05
to
If you don't want to get answer/help ....

Wes Harrison wrote:
>
>
>>DataRecord is not private here.
>
>
> Is that an issue?

It may be.

>
>>Where and how do you change data (DataRecord)?
>
>
> In a separate thread the data are loaded into DataRecord. The number of
> rows is increasing all the time (until all rows are retrieved from the
> database). Other than that there are no changes to the data. It's simply a
> program that retrieves some data and displays it in a JTable.
>>Do you rebuild TableModel when new data arrived?
>
>
> Rebuild? I fire that rows have been inserted but that is all. What do you
> mean by rebuild?

try fireTableDataChanged()

it should be something like:
when data ready

EventQueue.invokeLater(new Runnable(){
public void run(){
dr = new Dr;
model.fireTableDataChanged();
}
});

I will recomend you to have dr private
and one more method for your table model

public void loadDaata(DataRecord dr){
this.dr = dr;
super.fireTabelDataChanged();
}

and some where
public void dataArrived(final DataRecord dr){
EventQueue.invokeLater(new Runnable(){
public void run(){
model.loadData(dr);
}
});
}

and only if number of columns is static.

fireTableDataChanged and fireTableRowsInserted
work different.

remove synchronized - no needs

Roedy Green

unread,
Oct 21, 2005, 5:09:48 PM10/21/05
to
On Sat, 22 Oct 2005 02:29:44 +1000, "Wes Harrison" <nos...@home.com>
wrote or quoted :

>class MyTableModel extends AbstractTableModel {

How does any data get INTO your TableModel? I don't see any addRow
method.

Roedy Green

unread,
Oct 21, 2005, 5:14:07 PM10/21/05
to
On Sat, 22 Oct 2005 03:02:30 +1000, "Wes Harrison" <nos...@home.com>
wrote or quoted :

>In a separate thread the data are loaded into DataRecord.

But a TableModel must be in 100% in charge of the data. One of its
jobs it to notify Swing of the changes. If other threads are messing
with its data, it cannot do so.

The other thread must modify the data via a method of your TableModel.

JTables and TableModels are not thread safe, so typically you would
have that other thread do a little chunk of work at a time on the
Swing thread via SwingUtilities.invokeLater so as not to tie up the
Swing thread for long periods of time.

You could to that via a convenience method on your TableModel.

Roedy Green

unread,
Oct 21, 2005, 5:15:54 PM10/21/05
to
On Sat, 22 Oct 2005 02:23:08 +1000, "Wes Harrison" <nos...@home.com>
wrote or quoted :

>Geez, what's up your nose? I only responded honestly to Roedy's comments in


>what I thought was a civil and respectful manner. Obviously I am still
>doing something wrong or I wouldn't be getting the exception. Take a few
>Valium and lie down.

I don't think he meant to insult you. He was just saying he was at a
loss for more ideas in a Reader's Digest Toward More Pictureesque
Speech Way.

Wes Harrison

unread,
Oct 21, 2005, 6:48:24 PM10/21/05
to
The problem is resolved now. All I needed to do was to put the call to
fireTableRowsInserted() INSIDE my TableModel and all is well.

Thanks to all who assisted. Much appreciated.

Wes


jo...@vanderkogel.net

unread,
Oct 21, 2005, 6:58:21 PM10/21/05
to
> I don't think he meant to insult you. He was just saying he was at a
> loss for more ideas in a Reader's Digest Toward More Pictureesque
> Speech Way.

I did not mean to be harsh, I was only trying to hint at the fact that
rebuffing every suggestion someone makes and leaving it at that is not
a particularly constructive way to get someone to help you. Guess the
hint went lost somewhere.

Oh well...

Kind regards, Jonck

0 new messages