Java paintitem和indexoutofbounds错误
我已经创建了一个带有条形图的表格,它显示了文本中单词的频率。我显示了用户点击的特殊单词的数量或文本中整个单词的频率。我获取列表列表并将其发送给fill table函数。一切正常,但当我选择一个特殊的单词,然后单击以显示整个单词时,我会得到indexoutofbounds异常。我想这是因为我更改了数据源。这真的很奇怪,但很简单。然而,我无法解决它Java paintitem和indexoutofbounds错误,java,swt,runtime-error,Java,Swt,Runtime Error,我已经创建了一个带有条形图的表格,它显示了文本中单词的频率。我显示了用户点击的特殊单词的数量或文本中整个单词的频率。我获取列表列表并将其发送给fill table函数。一切正常,但当我选择一个特殊的单词,然后单击以显示整个单词时,我会得到indexoutofbounds异常。我想这是因为我更改了数据源。这真的很奇怪,但很简单。然而,我无法解决它 public void fill_count_table(List<RootWordSet> source){ final List
public void fill_count_table(List<RootWordSet> source){
final List<RootWordSet> mysource=source;
if(source!=null){
for(int i=0;i<source.size();i++){
TableItem ti=new TableItem(count_table, SWT.NONE);
ti.setText(source.get(i).getRoot());
}
count_table.addListener(SWT.PaintItem, new Listener() {
public void handleEvent(Event event) {
if (event.index == 1) {
try{
GC gc = event.gc;
TableItem item = (TableItem)event.item;
int index = count_table.indexOf(item);
System.out.println(mysource.size());
int percent = mysource.get(index).getWordNumber();
org.eclipse.swt.graphics.Color foreground = gc.getForeground();
org.eclipse.swt.graphics.Color background = gc.getBackground();
gc.setForeground(Display_1.getSystemColor(SWT.COLOR_BLUE));
gc.setBackground(Display_1.getSystemColor(SWT.COLOR_DARK_BLUE));
int width = (tc2.getWidth() - 1) * percent / 100;
gc.fillGradientRectangle(event.x, event.y, width, event.height, true);
Rectangle rect2 = new Rectangle(event.x, event.y, width-1, event.height-1);
gc.drawRectangle(rect2);
gc.setForeground(Display_1.getSystemColor(SWT.COLOR_RED));
String text = Integer.toString(percent) ;
Point size = event.gc.textExtent(text);
int offset = Math.max(0, (event.height - size.y) / 2);
gc.drawText(text, event.x+2, event.y+offset, true);
gc.setForeground(background);
gc.setBackground(foreground);
}
catch(Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
});
}
else{
count_table.removeAll();
count_table.redraw();
}
}
public void fill\u count\u表(列表源){
最终列表mysource=source;
如果(源!=null){
对于(int i=0;i更改此行:
int percent = mysource.get(index).getWordNumber();
到
int percent = mysource.get(index-1).getWordNumber();