简体   繁体   English

使用JavaFX中的FileChooser禁用隐藏文件

[英]Disable hidden files using FileChooser from JavaFX

I'm developing a Java app using JavaFX for it's user interface. 我正在使用JavaFX为其用户界面开发Java应用程序。

When I use the FileChooser class to load a CSV file from the computer hard drive in Os X Mavericks the dialog shows me all the files and folders, even the hidden one creating a lot of noise and making really hard to find the desired file. 当我使用FileChooser类从Os X Mavericks中的计算机硬盘驱动器加载CSV文件时,对话框会显示所有文件和文件夹,甚至隐藏的文件和文件夹会产生很多噪音,并且很难找到所需的文件。

This screenshot illustrates what I'm talking about: 这个截图说明了我在说什么:

在此输入图像描述

I think this could be more a OS X issue, but I don't understand how to fix, at least I don't understand how I can fix it with JavaFX FileChooser class. 我认为这可能更像是一个OS X问题,但我不明白如何解决,至少我不明白如何用JavaFX FileChooser类修复它。

Here's my code: 这是我的代码:

Stage stage = new Stage();
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open File");
fileChooser.getExtensionFilters().addAll(
        new FileChooser.ExtensionFilter("Comma-Separated Values (CSV)", "*.csv")
);
fileChooser.setInitialDirectory(
        new File(System.getProperty("user.home"))
);
File selectedFile = fileChooser.showOpenDialog(stage);

Update 更新

This is the Google Chrome modal to open files. 这是用于打开文件的Google Chrome模式。

在此输入图像描述

After looking at sources... FileChooser is implemented over native dialogs and completely non customizable. 查看源代码后... FileChooser是在原生对话框上实现的,完全不可自定义。

So, no, you can't force dialog to show/hide hidden files. 所以,不,你不能强制对话框显示/隐藏隐藏文件。

Inside FileChooser dialog there should be an context menu item to show/hide them, but you can't control this option from application FileChooser对话框中,应该有一个上下文菜单项来显示/隐藏它们,但是您无法从应用程序控制此选项

For example, here is how it looks on Linux: 例如,以下是它在Linux上的外观:

FileChooser上下文菜单

Have you tried hiding all hidden files in the Finder -program on the OS X machine? 您是否尝试将所有隐藏文件隐藏在OS X计算机上的Finder程序中? So: not using JavaFX, but using the program Finder? 那么:不使用JavaFX,而是使用程序Finder?

I know this is late to the game but none of the answers really explain the situation for MacOS. 我知道这是游戏的后期,但没有一个答案真正解释了MacOS的情况。

The open/save dialogs in JavaFX are native dialog (implemented as "sheets"). JavaFX中的打开/保存对话框是本机对话框(实现为“工作表”)。 Just as Finder does not have a "show hidden files" option, these dialogs don't either. 就像Finder没有“显示隐藏文件”选项一样,这些对话框也没有。 There are several solutions: 有几种解决方案:

  1. Relatively unknown keyboard shortcuts, available since Mavericks: View Hidden Files in Mac Open and Save Dialog Boxes . 相对未知的键盘快捷键,自Mavericks以来可用: 在Mac打开和保存对话框中查看隐藏文件 (I just stumbled on this; I have always used #2 below.) (我只是偶然发现了这个;我总是使用下面的#2。)

  2. Command-line switch in Terminal: 终端中的命令行开关:

    • show hidden -> defaults write com.apple.finder AppleShowAllFiles YES , or show hidden - > defaults write com.apple.finder AppleShowAllFiles YES ,或
    • hide -> defaults write com.apple.finder AppleShowAllFiles NO hide - > defaults write com.apple.finder AppleShowAllFiles NO

Your user has to be sufficiently aware of these alternatives; 您的用户必须充分了解这些替代方案; you cannot customize the native open/save dialog to provide a button to do this. 您无法自定义本机打开/保存对话框以提供执行此操作的按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM