DirectoryChooser Javafx buged?

2019-07-15 07:54发布

问题:

I'm using Directory Chooser for set a TextField with the selected path. If I choose some directory like Desktop, or any other folder it returns the path, BUT if I choose the directorys from my library like my Documents (C:\Users\Victor\Documents) it returns null. Is it a bug?? I'm using the code below:

        DirectoryChooser chooser = new DirectoryChooser();

        chooser.setTitle("Selecione o diretório");

        File defaultDirectory = new File(folderChooser);

        chooser.setInitialDirectory(defaultDirectory);

        File selectedDirectory = chooser.showDialog(null);

        pathFolderTxtField.setText(selectedDirectory.getAbsolutePath());

        folderChooser = selectedDirectory.getPath();

回答1:

This is a bug.

It is fixed in JavaFX 2.2.6 and Java 8 (both not yet released):

RT-28571 Choose a Windows 7 Library in a DirectoryChooser results in a COM Error

The fix doesn't actually allow library selection. If the user tries to select a library, instead the JavaFX system pops up an alert and prevents the directory chooser from closing:

Sample code I ran on JavaFX 2.2.4 (which gave an error) and the early access Java 8b77 (which showed the alert message instead) was:

import java.io.File;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;

public class DirectoryChooserTest extends Application {
  @Override public void start(final Stage stage) {    
    final TextField pathField = new TextField("C:/Users");
    pathField.setPrefWidth(250);

    Button chooseButton = new Button("Choose");
    chooseButton.setOnAction(new EventHandler<ActionEvent>() {
      @Override public void handle(ActionEvent t) {
        chooseDirectory(stage, pathField);
      }
    });

    VBox layout = new VBox(10);
    layout.getChildren().addAll(pathField, chooseButton);
    layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 10;");

    stage.setScene(new Scene(layout));
    stage.show();
  }

  private void chooseDirectory(Stage stage, TextField pathField) {
    DirectoryChooser chooser = new DirectoryChooser();
    chooser.setTitle("Selecione o diretório");
    chooser.setInitialDirectory(new File(pathField.getText()));
    File selectedDirectory = chooser.showDialog(stage);
    if (selectedDirectory != null) {
      pathField.setText(selectedDirectory.getAbsolutePath());
    }  
  }

  public static void main(String[] args) { launch(args); }
}

Error reported is:

E r r o r : 8 0 0 7 0 0 5 7   i n   p F i l e - > G e t D i s p l a y N a m e ( S I G D N _ F I L E S Y S P A T H ,   & p a t h ) 
 C O M   E r r o r : 8 0 0 7 0 0 5 7   T h e   p a r a m e t e r   i s   i n c o r r e c t . 

To get the error you need to choose a library from the left hand panel of the directory chooser dialog.