2013-01-10 27 views
8

Tôi đang cố gắng mở một FileChooser javafx trong thư mục người dùng theo một ví dụ tôi tìm thấy here.mở một FileChooser javafx trong thư mục người dùng

Đây là một đoạn mã đơn giản, tôi đang sử dụng:

FileChooser fc = new FileChooser(); 
fc.setTitle("Open Dialog"); 
String currentDir = System.getProperty("user.dir") + File.separator; 
file = new File(currentDir); 
fc.setInitialDirectory(file); 

Tuy nhiên, tôi giữ lấy cảnh báo này (đường dẫn đầy đủ hồ sơ đã được cắt ngắn):

Invalid URL passed to an open/save panel: '/Users/my_user'. Using 'file://localhost/Users/my_user/<etc>/' instead. 

Tôi xác nhận rằng Đối tượng file là một thư mục hiện có thêm các dòng sau:

System.out.println(file.exists()); //true 
System.out.println(file.isDirectory()); //true 

T Tôi không biết tại sao tôi lại nhận được thông báo cảnh báo.

UPDATE:

này có vẻ là một lỗi trong JavaFX: https://bugs.openjdk.java.net/browse/JDK-8098160 (bạn cần phải tạo một tài khoản miễn phí Jira để xem báo cáo lỗi). Sự cố này xảy ra trong OSX, không có ý tưởng về các nền tảng khác.

Trả lời

0

Hãy thử:

String currentDir = System.getProperty("user.home"); 
file = new File(currentDir); 
fc.setInitialDirectory(file); 
+0

cố gắng nhưng vấn đề vẫn còn đó – Sergio

+0

Were bạn có thể giải quyết vấn đề này? ? Tôi đã gặp phải vấn đề tương tự .. – lochi

+0

hi @lochi, hãy xem cập nhật của tôi cho câu hỏi – Sergio

6

Đây là những gì tôi đã kết thúc làm và nó làm việc như một nét duyên dáng.

Ngoài ra, hãy đảm bảo thư mục của bạn có thể truy cập được khi đọc nó (thực hành tốt). Bạn có thể tạo tệp và sau đó kiểm tra xem bạn có thể đọc tệp hay không. Sau đó, mã đầy đủ sẽ trông như thế này, mặc định là c: ổ đĩa nếu bạn không thể truy cập thư mục người dùng.

FileChooser fileChooser = new FileChooser(); 

//Extention filter 
FileChooser.ExtensionFilter extentionFilter = new FileChooser.ExtensionFilter("CSV files (*.csv)", "*.csv"); 
fileChooser.getExtensionFilters().add(extentionFilter); 

//Set to user directory or go to default if cannot access 
String userDirectoryString = System.getProperty("user.home"); 
File userDirectory = new File(userDirectoryString); 
if(!userDirectory.canRead()) { 
    userDirectory = new File("c:/"); 
} 
fileChooser.setInitialDirectory(userDirectory); 

//Choose the file 
File chosenFile = fileChooser.showOpenDialog(null); 
//Make sure a file was selected, if not return default 
String path; 
if(chosenFile != null) { 
    path = chosenFile.getPath(); 
} else { 
    //default return value 
    path = null; 
} 

này hoạt động trên Windows và Linux, nhưng có thể khác nhau trên các hệ điều hành khác (không kiểm tra)

0
@FXML private Label label1; //total file path print 
@FXML private Label labelFirst; //file dir path print 

private String firstPath; //dir path save 

public void method() { 
    FileChooser fileChooser = new FileChooser(); 
    if (firstPath != null) { 
     File path = new File(firstPath); 
     fileChooser.initialDirectoryProperty().set(path); 
    } 
    fileChooser.getExtensionFilters().addAll(
      new ExtensionFilter("Text Files", "*.txt"), 
      new ExtensionFilter("Image Files", "*.png", "*.jpg", "*.gif"), 
      new ExtensionFilter("Audio Files", "*.wav", "*.mp3", "*.aac"), 
      new ExtensionFilter("All Files", "*.*")); 
    File selectFile = fileChooser.showOpenDialog(OwnStage); 
    if (selectFile != null){ 
     String path = selectFile.getPath(); 
     int len = path.lastIndexOf("/"); //no detec return -1 
     if (len == -1) { 
      len = path.lastIndexOf("\\"); 
     } 
     firstPath = path.substring(0, len); 
     labelFirst.setText("file path : " + firstPath); 
     label1.setText("First File Select: " + path); 
    } 

    } 
Các vấn đề liên quan