Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / October 2005

Tip: Looking for answers? Try searching our database.

Exception in Swing code using a JTable

Thread view: 
Wes Harrison - 20 Oct 2005 14:12 GMT
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 - 20 Oct 2005 14:49 GMT
> 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

Signature

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/

Wes Harrison - 21 Oct 2005 14:58 GMT
> > 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
[quoted text clipped - 14 lines]
> 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
Vova Reznik - 20 Oct 2005 14:51 GMT
Do you use EventQueue.invokeLater() to fill table?

> 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
[quoted text clipped - 45 lines]
>
> Wes
Wes Harrison - 21 Oct 2005 14:59 GMT
> Do you use EventQueue.invokeLater() to fill table?

Yes, I do now.  Same problem though.

Wes
Vova Reznik - 21 Oct 2005 15:25 GMT
>>Do you use EventQueue.invokeLater() to fill table?
>
> Yes, I do now.  Same problem though.
>
> Wes

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
Wes Harrison - 21 Oct 2005 17:29 GMT
> >>Do you use EventQueue.invokeLater() to fill table?
> >
[quoted text clipped - 9 lines]
>
> Show example

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 - 21 Oct 2005 17:53 GMT
>>>>Do you use EventQueue.invokeLater() to fill table?
>>>
[quoted text clipped - 61 lines]
>
> Wes

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 - 21 Oct 2005 18:02 GMT
> >>>>Do you use EventQueue.invokeLater() to fill table?
> >>>
[quoted text clipped - 61 lines]
> >
> > Wes

> DataRecord is not private here.

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 - 21 Oct 2005 19:09 GMT
If you don't want to get answer/help ....

>>DataRecord is not private here.
>
> Is that an issue?

It may be.

>>Where and how do you change data (DataRecord)?
>
[quoted text clipped - 6 lines]
> 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

>>Why all methods are synchronized?
>
[quoted text clipped - 4 lines]
>
> Wes
Roedy Green - 21 Oct 2005 22:14 GMT
>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.
Signature

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

Roedy Green - 21 Oct 2005 22:09 GMT
>class MyTableModel extends AbstractTableModel {

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

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

Roedy Green - 21 Oct 2005 00:33 GMT
>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.

Signature

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

Wes Harrison - 21 Oct 2005 15:01 GMT
> >Exception in thread "AWT-EventQueue-0"
> >java.lang.ArrayIndexOutOfBoundsException: 1 >= 1
[quoted text clipped - 9 lines]
> 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
jonck@vanderkogel.net - 21 Oct 2005 16:30 GMT
> 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.
Wes Harrison - 21 Oct 2005 17:23 GMT
> > I am not using a Vector to hold the data.  I use an ArrayList.
>
[quoted text clipped - 11 lines]
> 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
Roedy Green - 21 Oct 2005 22:15 GMT
>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.
Signature

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

jonck@vanderkogel.net - 21 Oct 2005 23:58 GMT
> 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
Wes Harrison - 21 Oct 2005 23:48 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.