我有一个Swing应用程序,该应用程序显示带有JTree的文件系统。双击文件时,内容应该显示在JTabbedPane的JTextArea中。我有一个自定义的SwingWorker,它似乎可以正确处理小型文件,但不适用于大型文件。
我相信我正确使用了EDT。在doInBackground()中,我将单击文件的每一行附加到字符串中并返回它,以便get()可以使用它。在done()中,我将字符串附加到JTextArea,然后将其添加到JTabbedPane。
对于大文件,在读取行的最后我会遇到此异常。发生此错误后,我无法再打开任何文件。在线程转储中,我看到正在创建新的SwingWorker线程,但是它们正在等待ThreadPoolExectuor做某事。我确定它与此异常有关,但不确定如何。制作数据数组时,该错误发生在DataBufferInt构造函数中。它看起来与制作图像有关,但是我没有使用任何图像。
public DataBufferInt(int size) {
super(STABLE, TYPE_INT, size);
data = new int[size];
bankdata = new int[1][];
bankdata[0] = data;
}
例外。
Exception in thread "AWT-EventQueue-0" java.lang.NegativeArraySizeException
at java.awt.image.DataBufferInt.<init>(DataBufferInt.java:75)
at java.awt.image.Raster.createPackedRaster(Raster.java:467)
at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1032)
at java.awt.image.BufferedImage.<init>(BufferedImage.java:351)
at com.apple.laf.AquaPainter$AquaSingleImagePainter.createImage(AquaPainter.java:193)
at com.apple.laf.AquaPainter$AquaSingleImagePainter.lambda$paintFromSingleCachedImage$0(AquaPainter.java:178)
at sun.awt.image.MultiResolutionCachedImage.getResolutionVariant(MultiResolutionCachedImage.java:66)
at sun.awt.image.MultiResolutionCachedImage.getBaseImage(MultiResolutionCachedImage.java:106)
at sun.awt.image.AbstractMultiResolutionImage.getWidth(AbstractMultiResolutionImage.java:82)
at sun.awt.image.MultiResolutionCachedImage.getWidth(MultiResolutionCachedImage.java:89)
at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:3262)
at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:3214)
at com.apple.laf.AquaPainter$AquaSingleImagePainter.paintFromSingleCachedImage(AquaPainter.java:186)
at com.apple.laf.AquaPainter$AquaSingleImagePainter.paint(AquaPainter.java:141)
at com.apple.laf.AquaPainter.paint(AquaPainter.java:90)
at com.apple.laf.AquaGroupBorder.paintBorder(AquaGroupBorder.java:68)
at com.apple.laf.AquaTabbedPaneUI.paintContentBorder(AquaTabbedPaneUI.java:661)
at com.apple.laf.AquaTabbedPaneUI.paint(AquaTabbedPaneUI.java:134)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
at javax.swing.JComponent.paintComponent(JComponent.java:780)
at javax.swing.JComponent.paint(JComponent.java:1056)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paint(JComponent.java:1065)
at javax.swing.JViewport.paint(JViewport.java:728)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paint(JComponent.java:1065)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5210)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1579)
at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1502)
at javax.swing.RepaintManager.paint(RepaintManager.java:1272)
at javax.swing.JComponent._paintImmediately(JComponent.java:5158)
at javax.swing.JComponent.paintImmediately(JComponent.java:4969)
at javax.swing.RepaintManager$4.run(RepaintManager.java:831)
at javax.swing.RepaintManager$4.run(RepaintManager.java:814)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:789)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:738)
at javax.swing.RepaintManager.access$1200(RepaintManager.java:64)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1732)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
线程转储。
"SwingWorker-pool-1-thread-3" #27 daemon prio=5 os_prio=31 tid=0x00007feffb972800 nid=0x7103 waiting on condition [0x000070000f827000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000006c04f3c08> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Locked ownable synchronizers:
- None
"SwingWorker-pool-1-thread-2" #25 daemon prio=5 os_prio=31 tid=0x00007ff00100a000 nid=0x73df waiting on condition [0x000070000f51e000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000006c04f3c08> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Locked ownable synchronizers:
- None
"TimerQueue" #24 daemon prio=5 os_prio=31 tid=0x00007ff0008e2000 nid=0x11817 waiting on condition [0x000070000f724000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000006c04e5470> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
at java.util.concurrent.DelayQueue.take(DelayQueue.java:211)
at javax.swing.TimerQueue.run(TimerQueue.java:174)
at java.lang.Thread.run(Thread.java:748)
Locked ownable synchronizers:
- <0x00000006c04f7100> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)
"SwingWorker-pool-1-thread-1" #23 daemon prio=5 os_prio=31 tid=0x00007feffb930800 nid=0x727b waiting on condition [0x000070000f621000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000006c04f3c08> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Locked ownable synchronizers:
- None
"JMX server connection timeout 21" #21 daemon prio=9 os_prio=31 tid=0x00007feffc281800 nid=0x7b07 in Object.wait() [0x000070000f41b000]
java.lang.Thread.State: TIMED_WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
at com.sun.jmx.remote.internal.ServerCommunicatorAdmin$Timeout.run(ServerCommunicatorAdmin.java:168)
- locked <0x00000006c01c96e0> (a [I)
at java.lang.Thread.run(Thread.java:748)
Locked ownable synchronizers:
- None
"RMI Scheduler(0)" #20 daemon prio=9 os_prio=31 tid=0x00007feffca02000 nid=0x7a0f waiting on condition [0x000070000f318000]
java.lang.Thread.State: TIMED_WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000006c01d47e0> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078)
at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1093)
at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Locked ownable synchronizers:
- None
这是代码的一部分。我不确定如何在较小的规模上重新创建它,但希望您对它的功能有所了解。我有一个MouseAdapter(或Listener?),可以双击调用。如果节点(文件)不是目录,请执行Swing Worker,以逐行收集文件的内容,然后将其附加到JTextField。
class TreeMouseListener extends MouseAdapter {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
TreeNode selectedNode = (TreeNode) fileSystem.getLastSelectedPathComponent();
File f;
if (!(f = selectedNode.getFile()).isDirectory()) {
new openFileWorker(f).execute();
}
}
}
}
class openFileWorker extends SwingWorker<String, Void> {
File file;
public openFileWorker(File f) {
super();
file = f;
}
@Override
protected String doInBackground() throws Exception {
BufferedReader reader = null;
String line;
String output = "";
if (!"gz".equals(getFileExtension(this.file.getAbsolutePath()))) {
try {
reader = new BufferedReader(new FileReader(file.getAbsolutePath()));
while ((line = reader.readLine()) != null) {
System.out.println(line);
output = output + line + "\n";
}
System.out.println(output);
return output;
} catch (FileNotFoundException ex) {
Logger.getLogger(DossierView.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(DossierView.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
reader.close();
} catch (IOException ex) {
Logger.getLogger(DossierView.class.getName()).log(Level.SEVERE, null, ex);
}
}
} else {
try {
GZIPInputStream gzip = new GZIPInputStream(new FileInputStream(this.file.getAbsolutePath()));
reader = new BufferedReader(new InputStreamReader(gzip));
while ((line = reader.readLine()) != null) {
//System.out.println(line);
//fileText.append(line + "\n");
output = output + line + "\n";
}
return output;
} catch (IOException ex) {
Logger.getLogger(DossierView.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
reader.close();
} catch (IOException ex) {
Logger.getLogger(DossierView.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
return null;
}
@Override
public void done() {
try {
System.out.println(Thread.currentThread().getName());
String text = (String) get();
JTextArea fileText = new JTextArea();
fileText.append(text);
tabPane.add(this.file.getName().split("\\.")[0], fileText);
} catch (InterruptedException | ExecutionException ex) {
Logger.getLogger(DossierView.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}