首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Exception >

Exception in thread AWT-EventQueue-0 java.lang.IndexOutOfBoundsException: Index:

2014-01-26 
主要是好象说 Exception in thread AWT-EventQueue-0 java.lang.IndexOutOfBoundsException: Index: 5, Siz

主要是好象说
Exception in thread AWT-EventQueue-0 java.lang.IndexOutOfBoundsException: Index: 5, Size: 5
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at ynu.edu.cn.TableThread$EvenOddRendererY.getTableCellRendererComponent(TableThread.java:36)
好象是越界了......

我的程序是一个jtable类型,下面这个程序为线程,在程序中一直运行着...主要是为了jtable变色用的.
(当我的程序运行时,我向那个jtable加入新的一行时没异常(原来jtable是空的),当又加入时就出异常,虽然程序也正常执行任务,但我觉得有异常还是不好.......)
由于我的jtable和线程知识掌握的都不太好,下面程序是我参考网上的代码写的

public class TableThread implements Runnable{


public static final DefaultTableCellRenderer DEFAULT_RENDERER = new DefaultTableCellRenderer();
class EvenOddRendererY implements TableCellRenderer {
Color background;

ArrayList <Integer> tablecolorflag=new ArrayList <Integer> ();//处理jtable每一行的颜色

public EvenOddRendererY(ArrayList <Integer> tablecolorflag) {
this.tablecolorflag =tablecolorflag ;
}


public synchronized Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
Component renderer = DEFAULT_RENDERER.getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, column);
((JLabel) renderer).setOpaque(true);

background = getColor(tablecolorflag.get(row));//异常说的就是这句,有错不知道为什么

renderer.setBackground(background);
return renderer;
}

public Color getColor(int flag) {
switch (flag) {
case 1:
return Color.GREEN;
case 2:
return Color.RED;
case 3:
return Color.BLUE;
default:
return Color.WHITE;
}
}


}
//下面这个大部分可以不用看,就是获得jtable的每行信息,给它指定相应的颜色,然后变色
public void run() {
int totalTableRow;
while(true){
ArrayList <Integer> tablecolor=new ArrayList <Integer> ();
totalTableRow=PizzaStoreGUI.table.getRowCount();//获得总的行数
int tableColumn2=10,tableColumn3=9;//指定特定的行和列,column1获得烤制速度的信息,另一个获得开始时间
Date timeEnough=new Date(System.currentTimeMillis());//获得当前时间
//Date timeEnough2=new Date(System.currentTimeMillis()-15*60*1000);//获得当前时间
for(int i=0;i <totalTableRow;i++){
String tempTime=(String) PizzaStoreGUI.table.getValueAt(i, tableColumn2);//获得结束时间
String tempstartTime=(String) PizzaStoreGUI.table.getValueAt(i, tableColumn3);//获得开始时间
Date tempdate = null,tempstartdate = null;
try {
tempdate=PizzaStoreGUI.sdf2.parse(tempTime);
tempstartdate=PizzaStoreGUI.sdf2.parse(tempstartTime);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(timeEnough.getTime()> tempstartdate.getTime()+15*60*1000)//大于15分钟用蓝色
tablecolor.add(new Integer(3));
else if(timeEnough.after(tempdate))//已经烤制结束但未超过15分钟
tablecolor.add(new Integer(2));
else
tablecolor.add(new Integer(1));

}
PizzaStoreGUI.table.setDefaultRenderer(Object.class, new EvenOddRendererY(
tablecolor));
PizzaStoreGUI.dtm1.fireTableDataChanged();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Thread.currentThread().interrupt();
}
}
}

以上程序是我参考写着,我对线程和jtable掌握都不够,虽然不考虑异常,程序能够正常执行,但我还是希望去掉异常,希望大家给予支援!
------解决方法--------------------------------------------------------