我正在使用Directory Chooser设置带有所选路径的TextField。 如果我选择某个目录,如桌面,或任何其他文件夹,它返回路径,但如果我选择我的库中的directorys,如我的文档(C:\ Users \ Victor \ Documents),它返回null。这是一个错误? 我正在使用以下代码:
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();
答案 0 :(得分:4)
这是一个错误。
在JavaFX 2.2.6和Java 8(两者都尚未发布)中修复了它:
RT-28571 Choose a Windows 7 Library in a DirectoryChooser results in a COM Error
修复程序实际上不允许选择库。如果用户尝试选择库,则JavaFX系统会弹出警报并阻止目录选择器关闭:
我在JavaFX 2.2.4(发出错误)上运行的示例代码和早期访问Java 8b77(显示警报消息)是:
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); }
}
报告的错误是:
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 .
要获取错误,您需要从目录选择器对话框的左侧面板中选择一个库。