在JFileChooser中禁用“打开”按钮? [英] Disable Open button in JFileChooser?
问题描述
我扩展了 JFileChooser
并覆盖了 approveSelection
方法,以便当用户选择无效目录然后单击在打开按钮上,将显示 JOptionPane
中的错误消息。但是我想让我的 JFileChooser
更加用户友好,当用户点击无效目录然后在用户点击时重新启用时,打开禁用按钮一个有效的目录。是否可以进一步自定义我的 JFileChooser
并访问打开按钮,以便我可以相应地更改按钮的状态(可能通过侦听目录的侦听器)选择)?
I extended a JFileChooser
and overrode the approveSelection
method so that when a user chooses an invalid directory and then clicks on the Open button, an error message in a JOptionPane
will be displayed. But I want to make my JFileChooser
more user-friendly and make the Open button become disabled when a user clicks on an invalid directory and then become re-enabled when a user clicks on a valid directory. Is it possible to customize my JFileChooser
even further and get access to the Open button so that I can change the status of the button accordingly (possibly via a listener that listens for a directory selection)?
public class MyFileChooser extends JFileChooser {
private final JFrame mainFrame;
public MyFileChooser(JFrame mainFrame) {
this.mainFrame = mainFrame;
setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
}
@Override
public void approveSelection() {
if (/* directory is valid */) {
super.approveSelection();
return;
}
JOptionPane.showMessageDialog(mainFrame, "Invalid directory", "Error", JOptionPane.ERROR_MESSAGE);
}
}
推荐答案
当检测到文件/目录上的任何选择更改时,您可以通过调用chooser.setControlButtonsAreShown(false)来隐藏接受/取消按钮:
you can hide the accept/cancel buttons by calling chooser.setControlButtonsAreShown(false) when detect any selecting change on files/directories:
myChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
myChooser.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(evt.getPropertyName())) {
File file = (File) evt.getNewValue();
if (file != null && file.isFile()) { // your condition
myChooser.setControlButtonsAreShown(false);
}
else if ( file != null ) {
System.out.println(file.getName());
myChooser.setControlButtonsAreShown(true);
}
}
myChooser.repaint();
}
});
但它可能会让用户感到困惑,更好地制作自定义FileFilter并只显示你的文件/目录需要:
but it may confuse the user, its better to make custom FileFilter and showing only the files/directories you need:
public static class MyDirectoryFilter extends javax.swing.filechooser.FileFilter {
@Override
public boolean accept( File file ) {
return file.isDirectory() && customeCondition(file) ;
}
@Override
public String getDescription() {
return "this only my custom dir";
}
}
myChooser.setFileFilter( new MyDirectoryFilter () );
编辑:
我找到了一种禁用按钮的方法,通过迭代组件并获得打开按钮的句柄:
http://www.coderanch.com/t/468663/GUI/java/Disabling-Enabling-level-button-folder
示例:
myChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
myChooser.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(evt.getPropertyName())) {
File file = (File) evt.getNewValue();
if (file != null && file.isFile()) {
setOpenButtonState(myChooser, false);
}
else if ( file != null ) {
setOpenButtonState(myChooser, true);
System.out.println(file.getName());
}
}
myChooser.repaint();
}
});
public static void setOpenButtonState(Container c, boolean flag) {
int len = c.getComponentCount();
for (int i = 0; i < len; i++) {
Component comp = c.getComponent(i);
if (comp instanceof JButton) {
JButton b = (JButton) comp;
if ( "Open".equals(b.getText()) ) {
b.setEnabled(flag);
}
} else if (comp instanceof Container) {
setOpenButtonState((Container) comp, flag);
}
}
}
这篇关于在JFileChooser中禁用“打开”按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!