jFileChooser 스윙 창을 사용해야하는 프로젝트에서 작업하고 있습니다. "취소"또는 "열기"를 클릭하면 창이 닫히지 않습니다. 여기 StackOverflow에서 여러 개의 게시물을보고 JFileChooser tutorial 및 documentation을 보면이 반복되는 문제의 원인을 놓치고 있습니다.JFileChooser가 올바르게 닫히지 않습니다.
NetBeans와 함께 스윙 편집기를 사용하고 있습니다. 나는 또한 이클립스에서이 프로그램을 테스트 해 본 결과 동일한 결과를 얻었다.
새로운 업데이트 :
여기에 코드의 SSCCE 버전에서 내 시도는, 양해 해 주셔서 감사합니다 :
//package imi_test;
import java.io.*;
import java.io.IOException;
import javax.swing.*;
public class FileSelector extends JFrame {
//private static BufferedWriter out;
//private static FileInputStream in;
private static String selPath;
private int val;
//private static UniqueReader2 ur;
public FileSelector() throws IOException {
initComponents();
}
public static String getSelPath(){
return selPath;
}
public static void writeNewPath(String path) throws IOException{
//This would write the selected folder's new unique file path to a file
System.out.println("New file path written");
/*
out = new BufferedWriter(new FileWriter("recentPaths.txt",true));
in = new FileInputStream("recentPaths.txt");
ur = new UniqueReader(new InputStreamReader(in));
ur.main(null);
try{
if(ur.linePres){
}else{
out.write(path);
out.newLine();
out.flush();
out.close();
FileChecker.setFilePath(path);
}
}catch(Exception e){System.err.println(e);}
*/
}
//*******************************************************
//Netbeans auto gen GUI code starts here
@SuppressWarnings("unchecked")
private void initComponents() {
jDialog1 = new javax.swing.JDialog();
jMenuItem1 = new javax.swing.JMenuItem();
jFrame1 = new javax.swing.JFrame();
folderSelector = new javax.swing.JFileChooser();
javax.swing.GroupLayout jDialog1Layout = new javax.swing.GroupLayout(jDialog1.getContentPane());
jDialog1.getContentPane().setLayout(jDialog1Layout);
jDialog1Layout.setHorizontalGroup(
jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jDialog1Layout.setVerticalGroup(
jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
jMenuItem1.setText("jMenuItem1");
javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
jFrame1.getContentPane().setLayout(jFrame1Layout);
jFrame1Layout.setHorizontalGroup(
jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jFrame1Layout.setVerticalGroup(
jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
folderSelector.setCurrentDirectory(new java.io.File("Computer"));
folderSelector.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
folderSelector.setAcceptAllFileFilterUsed(false);
folderSelector.setDialogTitle("Please Select a Folder");
val = folderSelector.showOpenDialog(FileSelector.this);
folderSelector.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
folderSelectorActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(folderSelector, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(folderSelector, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
);
pack();
}
//Netbeans auto gen GUI code ends here
//*******************************************************
private void folderSelectorActionPerformed(java.awt.event.ActionEvent evt) {
if (val== JFileChooser.APPROVE_OPTION) {
try{
selPath = folderSelector.getSelectedFile().getPath();
writeNewPath(selPath);
this.dispose();
}catch(Exception ex){System.err.println(ex.getMessage() + "After folder selection");}
}
else{
selPath = null;
this.dispose();
}
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception ex) {
java.util.logging.Logger.getLogger(FileSelector.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try{
new FileSelector().setVisible(true);
}catch(Exception e){System.err.println(e.getMessage());}
}
});
}
// Variables declaration - do not modify
private javax.swing.JFileChooser folderSelector;
private javax.swing.JDialog jDialog1;
private javax.swing.JFrame jFrame1;
private javax.swing.JMenuItem jMenuItem1;
// End of variables declaration
}
이 시간 내 주셔서 감사하고이 문제에 도움이됩니다.
해결책 : 넣어 넷빈즈 파일에서 여분의 물건을 삭제 한 후, (살) 가지는 JDialog, JFrame, 및 JMenuItem를, 그것은이에 가정되는 방식으로 작동합니다.
더 나은 도움을 받으려면 [SSCCE] (http://sscce.org/)를 게시하십시오. –
@AndrewThompson 이것이 SSCCE 표준을 충족합니까? 또한이 문제를 해결할 수있는 방법에 대한 아이디어가 있습니까? – Jon