2010-11-12 38 views
1

我想創建一個簡單的基於控制檯的Java應用程序,它需要用戶從他們的本地文件系統中選擇文件。JavaVM Windows 7 64位 - JFileChooser()不顯示對話框

控制檯提示用戶選擇其中一個可用選項,然後打開給定的輸入。

public Client() throws UnknownHostException, IOException { 
    printuseroptions(); 
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
    char userdecision = br.readLine().charAt(0); 

    System.out.println(userdecision); 

    switch(userdecision){ 
     case '1': 
      System.out.println("Which file would you like to open?"); 
      openfile(br.readLine()); 
      break; 
     case '2': 
      System.out.println("Which file would you like to close?"); 
      closefile(br.readLine()); 
      break; 
     } 

private boolean openfile(String path){ 
    System.out.println("openfile("+path+")"); 
    return false; 
} 

private boolean closefile(String path){ 
    System.out.println("closefile("+path+")"); 
    new JFileChooser().showOpenDialog(null); 
    return false; 
} 

無論我做什麼,JFileChooser彈出框都不會打開。沒有顯示錯誤消息在控制檯上,但調試步進式顯示以下錯誤:

Blockquote Thread [main] (Suspended)
ClassNotFoundException(Throwable).(String, Throwable) line: 217
ClassNotFoundException(Exception).(String, Throwable) line: not available ClassNotFoundException.(String) line: not available
URLClassLoader$1.run() line: not available
AccessController.doPrivileged(PrivilegedExceptionAction, AccessControlContext) line: not available [native method]
Launcher$ExtClassLoader(URLClassLoader).findClass(String) line: not available
Launcher$ExtClassLoader.findClass(String) line: not available
Launcher$ExtClassLoader(ClassLoader).loadClass(String, boolean) line: not available Launcher$AppClassLoader(ClassLoader).loadClass(String, boolean) line: not available Launcher$AppClassLoader.loadClass(String, boolean) line: not available
Launcher$AppClassLoader(ClassLoader).loadClass(String) line: not available
ResourceBundle$RBClassLoader.loadClass(String) line: not available
CoreResourceBundleControl(ResourceBundle$Control).newBundle(String, Locale, String, ClassLoader, boolean) line: not available
ResourceBundle.loadBundle(CacheKey, List, Control, boolean) line: not available ResourceBundle.findBundle(CacheKey, List, List, int, Control, ResourceBundle) line: not available
ResourceBundle.getBundleImpl(String, Locale, ClassLoader, ResourceBundle$Control) line: not available
ResourceBundle.getBundle(String, ResourceBundle$Control) line: not available
Toolkit$3.run() line: not available AccessController.doPrivileged(PrivilegedAction) line: not available [native method]
Toolkit.() line: not available
Component.() line: not available
Client.closefile() line: 90 Client.() line: 60
Client.main(String[]) line: 36

相同的代碼完全運行在Linux 32位計算機上,所以我懷疑問題是相關的Windows。

下面的代碼在Windows和Linux上按預期運行,所以我懷疑可能是由於控制檯輸入在Windows vs Linux(CR LF)中處理的不同方式。

import javax.swing.JFileChooser; 

public class Example { 
    public static void main(String[] args) { 
     new JFileChooser().showOpenDialog(null); 
    } 
} 

感謝

回答

1

看起來,我很喜歡你只是爲新來的。 ;)讓我們看看我能否幫忙。

我對代碼進行了更改以使其能夠編譯並在Windows Server 2003 x64計算機上運行,​​但沒有看到任何問題 - 文件選擇器對話框打開。

我建議兩件事情可以做,以消除其他可能性:

1)保證了系統的本機外觀&感覺設置。你的程序啓動時使用此設置你的樣子&感覺系統默認:UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

2)確保你只構建並打開JFileChooserDialog,和所有其他Swing組件,在事件指派線程(EDT)內。如果您知道當前線程是主線程或其他工作線程(並且我認爲這是因爲您正在進行控制檯輸入),則需要撥打SwingUtilities.invokeLater(Runnable)才能正確執行。

祝你好運。

+0

嗨, 第二天發佈原始問題後,我找出了問題所在。 沒有問題! 對話框打開了,但它在IDE後面打開,但是沒有任何java圖標顯示在任務欄中,所以我從未意識到它實際上是打開的。 再次感謝。 – jmccrohan 2010-12-11 16:45:37

+0

是的 - 我發現這經常發生在Eclipse中,特別是在沒有任務欄圖標的對話框和啓動屏幕上。在JFileChooser上調用requestFocusInWindow()可能會有所幫助,但我無法確定。 – BoffinbraiN 2010-12-13 02:35:14