2014-11-21 90 views
0

从窗口中的按钮打开JFileChooser后,File Chooser打开,然后原始窗口关闭。我想在用户使用File Chooser时以及之后保持原始窗口始终打开。Java Swing:如何在关闭后保持父窗口打开JFileChooser

我的代码:

// Code from the class that makes the original window that has the launch button 
FilePicker filePicker = new FilePicker(); 
public void actionPerformed(ActionEvent e) { 
      txtImportLog.append("\nUser selecting file"); 
      if (filePicker.canPick()) { 
      filePicker.init(); 
      filePicker.getImportFile(); 
      } else { 
       txtImportLog.append("\nCan't pick more files."); 
      } 
     } 
    }); 

// Code from the class that creates a FilePicker 
//(yes, I know the getImportFile() and init() methods are setup badly, its just for 
// testing right now 
// Initialize - only should be called once 
public void init() { 
    filePicker = new JFileChooser(); 
    interval1 = 0; 
    interval2 = 0; 
    testFile = new File(""); // for testing. clearly. 
} 

// Get a file to import 
public static File getImportFile() { 
    filePicker.setFileSelectionMode(JFileChooser.FILES_ONLY); 
    filePicker.showOpenDialog(filePicker); 
    return filePicker.getSelectedFile(); 

} 
+4

考虑提供一个[可运行示例](https://stackoverflow.com/help/mcve),它演示了您的问题。这会导致更少的混淆和更好的响应 – MadProgrammer 2014-11-21 01:09:28

回答

0

哎呀,我根本就代码(由Eclipse插件的WindowBuilder自动生成),寻找当父窗口失去焦点,它将关闭应用程序。文件选择器的父项是主窗口。所以当用户点击“打开文件选择器”按钮时,主窗口的焦点将会丢失,关闭应用程序。

+1

你不会碰巧在某处放置代码吗? – luckydonald 2016-02-25 13:05:00

0

让我们看setDefaultCloseOperation。 http://docs.oracle.com/javase/7/docs/api/javax/swing/JFrame.html

设置当用户在此帧上启动“关闭”时默认发生的操作。您必须指定以下选项之一: DO_NOTHING_ON_CLOSE(在WindowConstants中定义):不要做任何事情;要求程序处理已注册WindowListener对象的windowClosing方法中的操作。 HIDE_ON_CLOSE(在WindowConstants中定义):在调用任何注册的WindowListener对象后自动隐藏框架。 DISPOSE_ON_CLOSE(在WindowConstants中定义):在调用任何已注册的WindowListener对象后自动隐藏并处理该帧。 EXIT_ON_CLOSE(在JFrame中定义):使用系统退出方法退出应用程序。仅在应用程序中使用它。 默认情况下,该值设置为HIDE_ON_CLOSE。对此属性值的更改会导致触发属性更改事件,并且属性名称为“defaultCloseOperation”。

相关问题