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
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/
>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.
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
Yes, I do now. Same problem though.
Wes
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
You (program) may change data structure (different number of columns for
example) with out notifying model about it.
Show example
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
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
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?
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
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
>class MyTableModel extends AbstractTableModel {
How does any data get INTO your TableModel? I don't see any addRow
method.
>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.
>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.
Thanks to all who assisted. Much appreciated.
Wes
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