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

Java Exception from inside

11 views
Skip to first unread message

Radwanski

unread,
Mar 31, 2009, 4:19:23 AM3/31/09
to

I made a JTable.
Actually I subclassed it to get rid JTable multtidifficult
functionality.
I just want to display my information in a table nothing more.

So far it works but when Java wants to repaint the Table
from inside some exception boil up.
Exception in thread "AWT-EventQueue-0"
java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
This only happens when rows are added.

I have no clue what causes the Exception.
I see now reference to my table so I can't debug it.
I pasted the Exception list at the end.

I subclassed DefaultTableModel and used it as in innerclass of
myTable.
I did this to get rid of the editable functionality of JTable
private class MyTableModel extends DefaultTableModel
{
public boolean isCellEditable(int row,int column)
{
return false;
}
}

To controll the look of MyTable I subclassed TableCellRenderer and
added
it also as an innerclass.
public MyTableRenderer()
{
super();
setOpaque(true); // background must show
}

public MyTableRenderer(CellContent content)
{
super();
setOpaque(true); // background must show
this.setBackground(content.getBackground());
this.setForeground(content.getForeground());
this.setFont(content.getFont());
this.setHorizontalAlignment(content.getHorizontalAlignment());
if (content.getLineBorder()!=null) this.setBorder
(content.getLineBorder());
this.setText(content.getValue());
}

public Component getTableCellRendererComponent( JTable table,
Object cellContent,
boolean isSelected,
boolean hasFocus,
int row,
int
column)
{
if (cellContent==null) return this;
CellContent content=(CellContent)cellContent; // We only cast
one time to speed up.
this.setBackground(content.getBackground());
this.setForeground(content.getForeground());
this.setFont(content.getFont());
this.setHorizontalAlignment(content.getHorizontalAlignment());
if (content.getLineBorder()!=null) { this.setBorder
(content.getLineBorder()); }
this.setText(content.getValue());
return this;
}
}

==============

Exception in thread "AWT-EventQueue-0"
java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.util.Vector.elementAt(Vector.java:427)
at javax.swing.table.DefaultTableModel.getValueAt
(DefaultTableModel.java:633)
at javax.swing.JTable.getValueAt(JTable.java:2695)
at javax.swing.JTable.prepareRenderer(JTable.java:5712)
at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:
2075)
at javax.swing.plaf.basic.BasicTableUI.paintCells
(BasicTableUI.java:1977)
at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:
1773)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java:143)
at javax.swing.JComponent.paintComponent(JComponent.java:763)
at javax.swing.JComponent.paint(JComponent.java:1027)
at javax.swing.JComponent.paintChildren(JComponent.java:864)
at javax.swing.JComponent.paint(JComponent.java:1036)
at javax.swing.JViewport.paint(JViewport.java:747)
at javax.swing.JComponent.paintChildren(JComponent.java:864)
at javax.swing.JComponent.paint(JComponent.java:1036)

at javax.swing.JComponent.paintChildren(JComponent.java:864)
at javax.swing.JComponent.paint(JComponent.java:1036)
at javax.swing.JComponent.paintChildren(JComponent.java:864)
at javax.swing.JComponent.paint(JComponent.java:1036)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:564)
at javax.swing.JComponent.paintChildren(JComponent.java:864)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5129)
at javax.swing.BufferStrategyPaintManager.paint
(BufferStrategyPaintManager.java:277)
at javax.swing.RepaintManager.paint(RepaintManager.java:1217)
at javax.swing.JComponent.paint(JComponent.java:1013)
at java.awt.GraphicsCallback$PaintCallback.run
(GraphicsCallback.java:21)
at sun.awt.SunGraphicsCallback.runOneComponent
(SunGraphicsCallback.java:60)
at sun.awt.SunGraphicsCallback.runComponents
(SunGraphicsCallback.java:97)
at java.awt.Container.paint(Container.java:1762)
at javax.swing.RepaintManager.paintDirtyRegions
(RepaintManager.java:814)
at javax.swing.RepaintManager.paintDirtyRegions
(RepaintManager.java:714)
at javax.swing.RepaintManager.seqPaintDirtyRegions
(RepaintManager.java:694)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run
(SystemEventQueueUtilities.java:128)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:
209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters
(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter
(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy
(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents
(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents
(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

Radwanski

unread,
Mar 31, 2009, 4:33:27 AM3/31/09
to
Oops I pressed return and sended to fast before I finished typing.

I made a JTable.
Actually I subclassed it to get rid JTable multtidifficult
functionality.
I just want to display my information in a table nothing more.


So far it works but when Java wants to repaint the Table
from inside some exception boil up.
Exception in thread "AWT-EventQueue-0"
java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
This only happens when rows are added.


I have no clue what causes the Exception.
I see now reference to my table so I can't debug it.
I pasted the Exception list at the end.

In next part I will send all code.

============

}


}


The idea of the renderer I got from example from sun collor table
example if I am not mistaken.

I also used an innerclass CellContent
It contains all information about each cell
Including borders font alignment etceteras and the String to dislplay

private class CellContent
{
private Color bgcolor=Color.WHITE; // background color
private Color color =Color.BLACK; // foreground color
private Font font =new Font(Font.MONOSPACED,Font.PLAIN,12);
private int alignment=JLabel.LEFT; // default alignment is left
private LineBorder lineBorder=null;
private String value ="";

public CellContent() {}
public CellContent(Color bgcolor,Color color,Font font,
int alignment,LineBorder lineBorder,String value )
{
if (bgcolor!=null) { this.bgcolor=bgcolor; }
if (color!=null) { this.color=color; }
if (font!=null) { this.font=font;}
this.alignment=alignment;
if (lineBorder!=null) {this.lineBorder=lineBorder;}
if (value!=null) { this.value=value;}
}

public Color getBackground() { return bgcolor; }
public Color getForeground() { return color; }
public Font getFont() { return font; }
public int getHorizontalAlignment() { return alignment; }
public LineBorder getLineBorder() { return lineBorder; }
public String getValue() { return value; }
public void setBackground(Color bgcolor) { this.bgcolor=bgcolor; }
public void setForeground(Color color) { this.color=color; }
public void setFont(Font font) { this.font=font; }
public void setHorizontalAlignment(int alignment)
{ this.alignment=alignment; }
public void setLineBorder(LineBorder lineBorder)
{ this.lineBorder=lineBorder;}
public void setValue(String value) { this.value=value; }
}

And finally the main methode for testing

public static void main(String[] args)
{
SimpleTable simpleTable=new SimpleTable();
simpleTable.addColumn("Column 1");
LineBorder lineBorder= new LineBorder(Color.GREEN,2,true); //
rounded
simpleTable.addColumn(Color.YELLOW,Color.GREEN,
new Font.BOLD,16),JLabel.LEFT,lineBorder,"Column 2");
simpleTable.addColumn("Column 3");

simpleTable.addRow();
// simpleTable.addRows(5);

// simpleTable.setColumnAlign(0);

JFrame frame=new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
JScrollPane jscrollPane=new JScrollPane(simpleTable);
frame.getContentPane().add(jscrollPane,BorderLayout.CENTER);
frame.pack();
frame.setExtendedState(frame.getExtendedState()|
JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);

// TODO, add your application code
System.out.println("Hello World!");

Radwanski

unread,
Mar 31, 2009, 4:35:23 AM3/31/09
to

// Any idea what casses the Exception to boil up ?

import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

public class SimpleTable extends JTable
{
private int columnLength=0; //number of rows
private int columns=0;
private MyTableModel myTableModel=new MyTableModel();

public SimpleTable()
{
super();
this.setModel(myTableModel);
this.getTableHeader().setReorderingAllowed(false);
this.getTableHeader().setResizingAllowed(false);
}


public void addColumn(String headerText)
{
LineBorder lineBorder= new LineBorder(Color.BLACK,4,false);
addColumn(new CellContent
(null,null,null,JLabel.CENTER,lineBorder,headerText));
}

public void addColumn(Color bgcolor,Color color,Font font,int
align,LineBorder lineBorder,String value )
{
addColumn(new CellContent
(bgcolor,color,font,align,lineBorder,value));
}

public void addColumn(CellContent content)
{
columns++;
TableColumn column = new TableColumn();
this.getColumnModel().addColumn(column);
MyTableRenderer myHeadRenderer=new MyTableRenderer(content);
column.setHeaderRenderer(myHeadRenderer);
}

public void addRow()
{
//Vector<String> stringVector = new Vector<String>();
Vector<CellContent> rowData = new Vector<CellContent>() ;
rowData.add (new CellContent()) ;
rowData.add (new CellContent()) ;
rowData.add (new CellContent()) ;
myTableModel.addRow(rowData) ;


// getModel().addRow(new Object[]{"v1", "v2", "v3"});

// addRows(1);
}

public void addRows(int number_of_rows)
{
for (int i=0;i<number_of_rows;i++)
{
columnLength++;
CellContent[] content=new CellContent[columns];
for (int column=0;column<columns;column++) content[column]=new
CellContent(); // default empty strings

myTableModel.addRow(content);
}
}

// Innerclass


private class MyTableModel extends DefaultTableModel
{

public MyTableModel()
{
super();
}

// when isCellEditable(int row,int column) returns false
// then the cell is not editable
// change code below if you want otherwise.


public boolean isCellEditable(int row,int column)
{
return false;
}
}


// Innerclass
// The renderer processes information for layout
// In here it is made an innerclass and hidden to outside to make
SimpleTable
// more easier to understand as JTable
private class MyTableRenderer extends JLabel implements
TableCellRenderer
{

// innerclass CellContent
// holds all information concering the cell
// such as colors fonts alginment.
// and a boolean that determines if the cell should be editable
// Also a value (reprecented as String)
// (It is parsed by myTableRendere)

public static void main(String[] args)
{

SimpleTable simpleTable=new SimpleTable();
simpleTable.addColumn("Column 1");
LineBorder lineBorder= new LineBorder(Color.GREEN,2,true); //
rounded
simpleTable.addColumn(Color.YELLOW,Color.GREEN,new Font

(Font.SERIF,Font.BOLD,16),JLabel.LEFT,lineBorder,"Column 2");

Radwanski

unread,
Mar 31, 2009, 5:32:21 AM3/31/09
to
Got it.

I changed

(private MyTableModel myTableModel=new MyTableModel();)


public void addColumn(CellContent content)
{
columns++;
TableColumn column = new TableColumn();
this.getColumnModel().addColumn(column);
MyTableRenderer myHeadRenderer=new MyTableRenderer
(content);
column.setHeaderRenderer(myHeadRenderer);
}

To:


public void addColumn(CellContent content)
{
columns++;
TableColumn column = new TableColumn();

//this.getColumnModel().addColumn(column);
myTableModel.addColumn(column);
MyTableRenderer myHeadRenderer=new MyTableRenderer(content);
column.setHeaderRenderer(myHeadRenderer);
}
It somehow had to do with what this.getColumnModel() returns.
It returns some unknown class with TableColumnModel interface.
This interface has some method addColumn

But actually I still not understand this method.
What is this method (that returns the Interface) has todo with JTable?
I don't know in which context it is used.

It seems to return an Interface but its method addColumn not seems to
work.
(At least not for me)


Nigel Wade

unread,
Mar 31, 2009, 9:03:57 AM3/31/09
to
Radwanski wrote:

>
> I made a JTable.
> Actually I subclassed it to get rid JTable multtidifficult
> functionality.
> I just want to display my information in a table nothing more.
>
> So far it works but when Java wants to repaint the Table
> from inside some exception boil up.
> Exception in thread "AWT-EventQueue-0"
> java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
> This only happens when rows are added.
>
> I have no clue what causes the Exception.
> I see now reference to my table so I can't debug it.
> I pasted the Exception list at the end.

My guess would be that you are modifying the table model on a thread other than
the EDT. Some operation you perform on the table model is triggering a redraw
of the table. Context switches from your thread to the EDT, before your thread
has finished modifying the table model, resulting in the table being redrawn
with an incompletely modified model.

--
Nigel Wade

0 new messages