我使用以下代码过滤Java Swing中的文件(使用Java 8)
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(false);
chooser.setControlButtonsAreShown(true);
chooser.setAcceptAllFileFilterUsed(false);
CustomFileFilter cffOne = new CustomFileFilter(null, "Directories Only", true);
CustomFileFilter cffTwo = new CustomFileFilter(".vi", "CAT Files (*.vi)", false);
CustomFileFilter cffThree = new CustomFileFilter(".hrd","HRRD Files (*.hrd)", false);
chooser.addChoosableFileFilter(cffOne);
chooser.addChoosableFileFilter(cffTwo);
chooser.addChoosableFileFilter(cffThree);
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.setDialogTitle("Select a directory or file(s)");
chooser.setApproveButtonText("OK");
SwingUtilities.updateComponentTreeUI(chooser);
PropertyChangeListener propertyChangeListener = new PropertyChange(chooser);
chooser.addPropertyChangeListener(propertyChangeListener);
int ret = chooser.showOpenDialog(null);
if (ret == JFileChooser.APPROVE_OPTION) {
// ..
}
过滤器工作正常。现在我的问题是当选择过滤器时,我们可以只禁用不匹配的文件并将它们显示为灰色而不是隐藏它们吗?