[英]IOException when opening JFileChooser
好吧,這個真的很奇怪。 每次我的應用程序第一次打開JFileChooser時,它都會拋出IOException,然后某些圖標無法正常顯示。
java.io.IOException
at sun.awt.image.GifImageDecoder.readHeader(GifImageDecoder.java:265)
at sun.awt.image.GifImageDecoder.produceImage(GifImageDecoder.java:102)
at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:246)
at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:172)
at sun.awt.image.ImageFetcher.run(ImageFetcher.java:136)
現在,當我深入研究錯誤時,它似乎在嘗試讀取標題時在一個圖標上,它只檢索前8個字節,這是不夠的。 我已經檢查了圖標文件,它們似乎都沒問題。 我試圖用另一個在此錯誤之前正確加載的圖標文件覆蓋圖標文件,但同樣的事情。
打破這個錯誤時,這是我的堆棧:
Daemon Thread [Image Fetcher 0] (Suspended (exception IOException))
GifImageDecoder.readHeader() line: 265 [local variables unavailable]
GifImageDecoder.produceImage() line: 102 [local variables unavailable]
ByteArrayImageSource(InputStreamImageSource).doFetch() line: 246
ImageFetcher.fetchloop() line: 172
ImageFetcher.run() line: 136 [local variables unavailable]
這是挖掘GifImageDecoder實例時的變量值。
source ByteArrayImageSource (id=272)
awaitingFetch false
consumers null
decoder GifImageDecoder (id=271)
decoders GifImageDecoder (id=271)
imagedata (id=307)
[0] 71
[1] 73
[2] 70
[3] 56
[4] 57
[5] 97
[6] 16
[7] 13
[8] 10
imagelength 9
imageoffset 0
通常,這個圖像數據應該更大。 前10個字節是標頭,但它只能檢索8個字節,如您所見。 在此異常之后,JFileChooser中的每個其他圖標都無法正確加載。
這是對readHeader()的正確調用:
source ByteArrayImageSource (id=208)
awaitingFetch false
consumers null
decoder GifImageDecoder (id=207)
decoders GifImageDecoder (id=207)
imagedata (id=223)
[0...99]
[100...199]
[200...299]
[300...399]
[400...499]
[500...599]
[600...699]
[700...799]
[800...899]
[900...979]
imagelength 980
imageoffset 0
緩沖區完全加載了一個圖標,正好在引發錯誤的圖標之前。
這是一個可能崩潰的例子(它發生在我的代碼的幾個部分,每當我第一次加載我的系統圖標時):
public class DirectoryBrowser extends JFileChooser{
private String suffixAccepted = null;
public DirectoryBrowser(File file, String chooserTitle, String approveOpenBtnText, String suffixAccepted)
{
super(file);
this.suffixAccepted = suffixAccepted;
init(chooserTitle, approveOpenBtnText);
}
當它進入超級(文件)時它會去那里:
Thread [AWT-EventQueue-0] (Suspended)
Object.wait(long) line: not available [native method]
MediaTracker.waitForID(int, long) line: 651
ImageIcon.loadImage(Image) line: 234
ImageIcon.<init>(byte[]) line: 215
SwingUtilities2$2.createValue(UIDefaults) line: 1105
UIDefaults.getFromHashtable(Object) line: 185
UIDefaults.get(Object) line: 130
MultiUIDefaults.get(Object) line: 44
MultiUIDefaults(UIDefaults).getIcon(Object) line: 411
UIManager.getIcon(Object) line: 613
IronFileChooserUI(BasicFileChooserUI).installIcons(JFileChooser) line: 233
IronFileChooserUI(BasicFileChooserUI).installDefaults(JFileChooser) line: 219
IronFileChooserUI(BasicFileChooserUI).installUI(JComponent) line: 135
IronFileChooserUI(MetalFileChooserUI).installUI(JComponent) line: 139
DirectoryBrowser(JComponent).setUI(ComponentUI) line: 653
DirectoryBrowser(JFileChooser).updateUI() line: 1757
DirectoryBrowser(JFileChooser).setup(FileSystemView) line: 366
DirectoryBrowser(JFileChooser).<init>(File, FileSystemView) line: 332
DirectoryBrowser(JFileChooser).<init>(File) line: 315
DirectoryBrowser.<init>(File, String, String, String) line: 33
PackToIntegratePanel.choosePackPathToIntegrateFile() line: 522
PackToIntegratePanel$1.actionPerformed(ActionEvent) line: 104
JButton(AbstractButton).fireActionPerformed(ActionEvent) line: 1849
AbstractButton$Handler.actionPerformed(ActionEvent) line: 2169
DefaultButtonModel.fireActionPerformed(ActionEvent) line: 420
DefaultButtonModel.setPressed(boolean) line: 258
BasicButtonListener.mouseReleased(MouseEvent) line: 236
JButton(Component).processMouseEvent(MouseEvent) line: 5517
JButton(JComponent).processMouseEvent(MouseEvent) line: 3135
JButton(Component).processEvent(AWTEvent) line: 5282
JButton(Container).processEvent(AWTEvent) line: 1966
JButton(Component).dispatchEventImpl(AWTEvent) line: 3984
JButton(Container).dispatchEventImpl(AWTEvent) line: 2024
JButton(Component).dispatchEvent(AWTEvent) line: 3819
LightweightDispatcher.retargetMouseEvent(Component, int, MouseEvent) line: 4212
LightweightDispatcher.processMouseEvent(MouseEvent) line: 3892
LightweightDispatcher.dispatchEvent(AWTEvent) line: 3822
WorkbenchFrame(Container).dispatchEventImpl(AWTEvent) line: 2010
WorkbenchFrame(Window).dispatchEventImpl(AWTEvent) line: 1791
WorkbenchFrame(Component).dispatchEvent(AWTEvent) line: 3819
EventQueue.dispatchEvent(AWTEvent) line: 463
EventDispatchThread.pumpOneEventForHierarchy(int, Component) line: 242
EventDispatchThread.pumpEventsForHierarchy(int, Conditional, Component) line: 163
EventDispatchThread.pumpEvents(int, Conditional) line: 157
EventDispatchThread.pumpEvents(Conditional) line: 149
EventDispatchThread.run() line: 110
然后上面提到的其他線程檢索圖標(參見本文的第2個堆棧)
你什么意思? 我不操縱任何隊列。
抱歉。 就像@kai說的那樣,我的意思是事件派遣線程(EDT) 。 確保在EDT上創建所有組件,包括JFileChooser
,就像它們在教程中所示 。 如果不這樣做,那將是某個文件系統和顯示器之間的競爭。
我剛剛注意到:沒有IronFileChooser
事情,它仍然會發生嗎?
我有一個非常相似的問題。 似乎從一個1.6.X版本開始,這是一個JFileChooser錯誤。 有時它會拋出這個內部異常,沒有明顯的理由。 我不確定它是否已經修復或者是否已正確記錄。 此外,我不確定這是否適用於您的情況,但它確實提醒我這個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.