gpt4 book ai didi

java - 在文件选择器中禁用“新建文件夹”按钮无法正常工作

转载 作者:行者123 更新时间:2023-12-02 10:54:58 25 4
gpt4 key购买 nike

我使用以下代码禁用新文件夹按钮:

 public void disableNewFolderButton( Container c ) {

System.out.print("in disable fn");
int len = c.getComponentCount();
for (int i = 0; i < len; i++) {
Component comp = c.getComponent(i);
if (comp instanceof JButton) {
JButton b = (JButton)comp;
Icon icon = b.getIcon();
if (icon != null
&& icon == UIManager.getIcon("FileChooser.newFolderIcon"))
{
System.out.print("in disable fn");
b.setEnabled(false);
}
}
else if (comp instanceof Container) {
disableNewFolderButton((Container)comp);
}
}
}

代码在以下几行中被调用:

   JFileChooser of=new JFileChooser();
of.setAcceptAllFileFilterUsed(false);
of.addChoosableFileFilter(new MyFilter());
disableNewFolderButton(of);

但仅当文件选择器首次显示时,新文件夹按钮才会被禁用。假设我转到某个驱动器,例如 g: ,然后该按钮再次启用。如何正确设置?

最佳答案

这对我有用......

    //Create a file chooser
UIManager.put("FileChooser.readOnly", Boolean.TRUE);
JFileChooser fc = new JFileChooser();

关于java - 在文件选择器中禁用“新建文件夹”按钮无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5568921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com