मैं अलग-अलग आमंत्रणों में संवाद की "वर्तमान निर्देशिका" कैसे बना सकता हूं?
आप इस
के लिए Singleton Pattern दृष्टिकोण को संशोधित कर सकता है जहां आप केवल एक FileChooser
और मॉनिटर का प्रयोग करेंगे/प्रारंभिक निर्देशिका नियंत्रित करते हैं, अभी तक सीधे वर्ग के बाहर संशोधनों को उदाहरण उजागर नहीं
उदाहरण के लिए:
public class RetentionFileChooser {
private static FileChooser instance = null;
private static SimpleObjectProperty<File> lastKnownDirectoryProperty = new SimpleObjectProperty<>();
private RetentionFileChooser(){ }
private static FileChooser getInstance(){
if(instance == null) {
instance = new FileChooser();
instance.initialDirectoryProperty().bindBidirectional(lastKnownDirectoryProperty);
//Set the FileExtensions you want to allow
instance.getExtensionFilters().setAll(new ExtensionFilter("png files (*.png)", "*.png"));
}
return instance;
}
public static File showOpenDialog(){
return showOpenDialog(null);
}
public static File showOpenDialog(Window ownerWindow){
File chosenFile = getInstance().showOpenDialog(ownerWindow);
if(chosenFile != null){
//Set the property to the directory of the chosenFile so the fileChooser will open here next
lastKnownDirectoryProperty.setValue(chosenFile.getParentFile());
}
return chosenFile;
}
public static File showSaveDialog(){
return showSaveDialog(null);
}
public static File showSaveDialog(Window ownerWindow){
File chosenFile = getInstance().showSaveDialog(ownerWindow);
if(chosenFile != null){
//Set the property to the directory of the chosenFile so the fileChooser will open here next
lastKnownDirectoryProperty.setValue(chosenFile.getParentFile());
}
return chosenFile;
}
}
यह FileChooser
की प्रारंभिक निर्देशिका सेट फ़ाइल उपयोगकर्ता पिछली बार खोले/बचाया की सूची होने के लिए जब यह फिर से इस्तेमाल किया है
उदाहरण उपयोग:
File chosenFile = RetentionFileChooser.showOpenDialog();
लेकिन वहाँ एक सीमा है यहाँ:
//Set the FileExtensions you want to allow
instance.getExtensionFilters().setAll(new ExtensionFilter("png files (*.png)", "*.png"));
के रूप में उपलब्ध कराने के बिना किसी भी ExtensionFilter
के FileChooser
हो जाता है कम उपयोगकर्ता के अनुकूल, उपयोगकर्ता की आवश्यकता होती है मैन्युअल appe के लिए फ़ाइल प्रकार, फिर भी उन्हें अद्यतन करने के किसी भी तरीके से उदाहरण प्रदान करते समय फिल्टर प्रदान करना
इस पर सुधार करने का एक तरीका RetentionFileChooser
के भीतर वैकल्पिक फ़िल्टर का खुलासा करना है, जिसे varargs का उपयोग करके प्रदान किया जा सकता है, जिससे आप जब फिल्टर संशोधित करने के लिए चुन सकते हैं जैसा कि आप संवाद
उदाहरण के लिए प्रदर्शित करते हैं, पिछले पर निर्माण:
public class RetentionFileChooser {
public enum FilterMode {
//Setup supported filters
PNG_FILES("png files (*.png)", "*.png"),
TXT_FILES("txt files (*.txt)", "*.txt");
private ExtensionFilter extensionFilter;
FilterMode(String extensionDisplayName, String... extensions){
extensionFilter = new ExtensionFilter(extensionDisplayName, extensions);
}
public ExtensionFilter getExtensionFilter(){
return extensionFilter;
}
}
private static FileChooser instance = null;
private static SimpleObjectProperty<File> lastKnownDirectoryProperty = new SimpleObjectProperty<>();
private RetentionFileChooser(){ }
private static FileChooser getInstance(FilterMode... filterModes){
if(instance == null) {
instance = new FileChooser();
instance.initialDirectoryProperty().bindBidirectional(lastKnownDirectoryProperty);
}
//Set the filters to those provided
//You could add check's to ensure that a default filter is included, adding it if need be
instance.getExtensionFilters().setAll(
Arrays.stream(filterModes)
.map(FilterMode::getExtensionFilter)
.collect(Collectors.toList()));
return instance;
}
public static File showOpenDialog(FilterMode... filterModes){
return showOpenDialog(null, filterModes);
}
public static File showOpenDialog(Window ownerWindow, FilterMode...filterModes){
File chosenFile = getInstance(filterModes).showOpenDialog(ownerWindow);
if(chosenFile != null){
lastKnownDirectoryProperty.setValue(chosenFile.getParentFile());
}
return chosenFile;
}
public static File showSaveDialog(FilterMode... filterModes){
return showSaveDialog(null, filterModes);
}
public static File showSaveDialog(Window ownerWindow, FilterMode... filterModes){
File chosenFile = getInstance(filterModes).showSaveDialog(ownerWindow);
if(chosenFile != null){
lastKnownDirectoryProperty.setValue(chosenFile.getParentFile());
}
return chosenFile;
}
}
उदाहरण उपयोग:
//Note the previous example still holds
File chosenFile = RetentionFileChooser.showOpenDialog();
File file = RetentionFileChooser.showSaveDialog(FilterMode.PNG_FILES);
लेकिन संवाद बंद या रद्द होने पर यह काम नहीं करता है।
दुर्भाग्य FileChooser
से पहले ही बंद हो गया क्या निर्देशिका की जांच की जा रही थी के बारे में जानकारी का खुलासा नहीं करता/समाप्त कर दिया। यदि आप कक्षा को संकलित करते हैं और ट्रेस करते हैं, तो अंत में यह native
कॉल हिट करता है।तो अगर FileChooser
final
को उप-वर्गीकृत करने की इजाजत नहीं दे रही थी, तो यह जानकारी प्राप्त करने में सक्षम होने की संभावना नहीं है
उपरोक्त दृष्टिकोण केवल एक ही लाभ प्रदान करता है कि यह प्रारंभिक निर्देशिका को नहीं बदलता है इस परिदृश्य
मारा जाता है आप रुचि रखते हैं, तो इस सवाल के भीतर native
कुंजी शब्द पर कुछ बहुत अच्छा जवाब और जुड़ा हुआ उन हैं:
फ़ाइल एक्सटेंशन सुझावों के लिए एक सरल समाधान फ़ाइल एक्सटेंशन को हर बार कॉल करने के लिए रीसेट करना होगा। आईई: 'fileChooser.getExtension फ़िल्टर() स्पष्ट(); fileChooser.getExtension फ़िल्टर()। जोड़ें (नया FileChooser.ExtensionFilter ("पीएनजी", "* .png")); ' –