问题
I have below SwingWorker for populate the jTable1. And i have button for executing this task.
public class WorkerTime extends SwingWorker<Void, Void> {
protected Void doInBackground() throws Exception {
DefaultTableModel model1 = (DefaultTableModel) jTable1.getModel();
model1.setRowCount(0);
for (int i = 0; i < 5; i++){
model1.addRow(new String[]{"a","a","a","a","a","a","a","a","a","a","a"});
model1.fireTableDataChanged();
}
return null;
}
public void done() {
}
}
First, second and sometimes third executing succesfully working. But sometimes when i click my button jTable cleared but desired 5 lines (a,a,a... lines) not coming and Java throw below error;
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1 >= 0
at java.util.Vector.elementAt(Vector.java:470)
at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:649)
at javax.swing.JTable.getValueAt(JTable.java:2717)
at javax.swing.JTable.prepareRenderer(JTable.java:5719)
at javax.swing.plaf.synth.SynthTableUI.paintCell(SynthTableUI.java:684)
at javax.swing.plaf.synth.SynthTableUI.paintCells(SynthTableUI.java:581)
at javax.swing.plaf.synth.SynthTableUI.paint(SynthTableUI.java:365)
at javax.swing.plaf.synth.SynthTableUI.update(SynthTableUI.java:276)
at javax.swing.JComponent.paintComponent(JComponent.java:778)
at javax.swing.JComponent.paint(JComponent.java:1054)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5219)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1529)
at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1452)
at javax.swing.RepaintManager.paint(RepaintManager.java:1249)
at javax.swing.JComponent._paintImmediately(JComponent.java:5167)
at javax.swing.JComponent.paintImmediately(JComponent.java:4978)
at javax.swing.RepaintManager$3.run(RepaintManager.java:808)
at javax.swing.RepaintManager$3.run(RepaintManager.java:796)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:769)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:718)
at javax.swing.RepaintManager.access$1100(RepaintManager.java:62)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1677)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
I don't understand whats going on. Also i can't debug it because problem occured randomly. Sometimes i execute the task and see data on the screen 5-6 times, sometimes 1-2 times. Any help appreciated. Thanks.
---EDITING---
My Button is;
if(radioButtonLF.isSelected() || radioButtonLF2.isSelected() || radioButtonT.isSelected() || radioButtonLH.isSelected()){
WorkerTime workerTime = new WorkerTime();
workerTime.execute();
}
if(!fTextFieldSB.getText().equals(null)){
WorkerSB workerSB = new WorkerSB();
workerSB.execute();
}
if(!fTextFieldP.getText().equals(null)){
WorkerPB workerPB = new WorkerPB();
workerPB.execute();
}
回答1:
Not sure whether this solves your problem, but you don't need to call fireTableDataChanged()
. This is alreday done inside addRow
.
回答2:
---SOLVED---
Problem detected in button event.
fTextFieldSB.getText()
and fTextFieldP.getText()
returns empty string like a ""
So if not return null, all if conditions fired in same time. We dont want it. If conditions should be;
if(radioButtonLF.isSelected() || radioButtonLF2.isSelected() || radioButtonT.isSelected() || radioButtonLH.isSelected()){
WorkerTime workerTime = new WorkerTime();
workerTime.execute();
}
if(!fTextFieldSB.getText().equals("")){
WorkerSB workerSB = new WorkerSB();
workerSB.execute();
}
if(!fTextFieldP.getText().equals("")){
WorkerPB workerPB = new WorkerPB();
workerPB.execute();
}
来源:https://stackoverflow.com/questions/30164166/jtable-populating-and-refreshing-arrayindexoutofboundsexception