[英]Changing selected files in a JFileChooser in response to an event
I would like my JFileChooser
to allow multiple file selection, but with a limit on the number of files that can be selected simultaneously. 我希望我的
JFileChooser
允许多个文件选择,但是可以同时选择的文件数量有限制。
Ideally I would like to constrain the selection in real-time, with priority given to the most-recently selected file, ie when a 3rd file is selected, the 1st file (ie the one that was selected earliest) should be deselected automatically. 理想情况下,我希望实时约束选择,优先考虑最近选择的文件,即当选择第3个文件时,应自动取消选择第1个文件(即最早选择的文件)。
I thought that a PropertyChangeListener
like this one would work: 我认为像这样的
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);
}
However the call to fc.setSelectedFiles()
has no effect (although it fires an event, it does not update the selection in the list.) 但是,对
fc.setSelectedFiles()
的调用没有任何效果(虽然它触发了一个事件,但它不会更新列表中的选择。)
Is there any way to programatically force a change to the selection while the JFileChooser
is open? 在
JFileChooser
打开时,有没有办法以编程方式强制改变选择? Or is there another way to limit the size of the selection? 或者是否有其他方法来限制选择的大小?
I have discovered that this bug is specific to the Macintosh look-and feel. 我发现这个bug特定于Macintosh的外观。
setSelectedFile
and setSelectedFiles
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. setSelectedFile
和setSelectedFiles
在Mac上根本不起作用(甚至在打开对话框之前。)我的示例代码可以正常使用Metal外观。
Possible workarounds include: 可能的解决方法包括:
FileDialog
instead of a JFileChooser
FileDialog
而不是JFileChooser
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.IOException;
import javax.swing.JFileChooser;
public class MyClass {
final static JFileChooser fc = new JFileChooser("/");
public static void main(String[] args) throws IOException {
fc.setMultiSelectionEnabled(true);
fc.addPropertyChangeListener(new ChangeListener());
fc.showOpenDialog(null);
}
private static class ChangeListener implements PropertyChangeListener{
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (JFileChooser.SELECTED_FILES_CHANGED_PROPERTY.equals(evt.getPropertyName())) {
File[] selectedFiles = fc.getSelectedFiles();
File[] allowedFiles = new File[2];
if (selectedFiles.length > 2) {
allowedFiles[0] = selectedFiles[1];
allowedFiles[1] = selectedFiles[0];
fc.setSelectedFiles(allowedFiles);
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.