更改 JFileChooser 中选定的文件以响应事件
我希望我的 JFileChooser 允许选择多个文件,但可以同时选择的文件数量受到限制。
理想情况下,我想实时限制选择,优先考虑最近选择的文件,即当选择第三个文件时,应自动取消选择第一个文件(即最早选择的文件)。
我认为像这样的 PropertyChangeListener
可以工作:
public static void main(String[] args) throws IOException {
final JFileChooser fc = new JFileChooser(didir);
fc.setMultiSelectionEnabled(true);
fc.addPropertyChangeListener(new PropertyChangeListener() {
private final Set<File> selected = Sets.newLinkedHashSet();
public void propertyChange(PropertyChangeEvent evt) {
if (JFileChooser.SELECTED_FILES_CHANGED_PROPERTY.equals(evt.getPropertyName())) {
File[] selectedFiles = fc.getSelectedFiles();
if (selectedFiles.length > 2) {
selected.addAll(Arrays.asList(selectedFiles));
int numToRemove = Math.max(0, selected.size() - 2);
Iterables.removeIf(Iterables.limit(selected, numToRemove),
Predicates.alwaysTrue());
fc.setSelectedFiles(selected.toArray(new File[0]));
}
}
}
});
fc.showOpenDialog(null);
}
但是对 fc.setSelectedFiles()
的调用没有效果(虽然它触发了一个事件,但它不会更新选择在列表中。)
是否有任何方法可以在 JFileChooser 打开时以编程方式强制更改选择?或者还有其他方法来限制选择的大小?
I would like my JFileChooser
to allow multiple file selection, but with a limit on the number of files that can be selected simultaneously.
Ideally I would like to constrain the selection in real-time, with priority given to the most-recently selected file, i.e. when a 3rd file is selected, the 1st file (i.e the one that was selected earliest) should be deselected automatically.
I thought that a PropertyChangeListener
like this one would work:
public static void main(String[] args) throws IOException {
final JFileChooser fc = new JFileChooser(didir);
fc.setMultiSelectionEnabled(true);
fc.addPropertyChangeListener(new PropertyChangeListener() {
private final Set<File> selected = Sets.newLinkedHashSet();
public void propertyChange(PropertyChangeEvent evt) {
if (JFileChooser.SELECTED_FILES_CHANGED_PROPERTY.equals(evt.getPropertyName())) {
File[] selectedFiles = fc.getSelectedFiles();
if (selectedFiles.length > 2) {
selected.addAll(Arrays.asList(selectedFiles));
int numToRemove = Math.max(0, selected.size() - 2);
Iterables.removeIf(Iterables.limit(selected, numToRemove),
Predicates.alwaysTrue());
fc.setSelectedFiles(selected.toArray(new File[0]));
}
}
}
});
fc.showOpenDialog(null);
}
However the call to fc.setSelectedFiles()
has no effect (although it fires an event, it does not update the selection in the list.)
Is there any way to programatically force a change to the selection while the JFileChooser
is open? Or is there another way to limit the size of the selection?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现这个错误是 Macintosh 外观所特有的。
setSelectedFile
和setSelectedFiles
在 Mac 上根本不起作用(甚至在打开对话框之前也是如此)。我的示例代码在 Metal 外观和感觉上运行良好。可能的解决方法包括:
使用(不支持多个文件选择)FileDialog
而不是JFileChooser
I have discovered that this bug is specific to the Macintosh look-and feel.
setSelectedFile
andsetSelectedFiles
do not work at all on the Mac (even before the dialog is opened.) My sample code works fine with the Metal look-and-feel.Possible workarounds include:
Use a(does not support multiple file selection)FileDialog
instead of aJFileChooser