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

发布于 2024-10-30 22:29:34 字数 897 浏览 0 评论 0原文

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

 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: ,然后该按钮再次启用。如何正确设置?

I disable the new Folder button using the following code:

 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);
  }
}
 }

The code is called in the following lines:

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

But the new folder button is disabled only when the file chooser is first displayed. Suppose i go to some drive , say g: , then the button is enabled again. How to set this right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夕色琉璃 2024-11-06 22:29:34

这对我有用......

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

this is working for me...

    //Create a file chooser
UIManager.put("FileChooser.readOnly", Boolean.TRUE); 
JFileChooser fc = new JFileChooser();
难得心□动 2024-11-06 22:29:34

禁用“新文件夹”操作(这又将禁用该按钮):

import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;

public class FileChooserAction
{
    public static void createAndShowUI()
    {
        JFileChooser chooser = new JFileChooser();

        BasicFileChooserUI ui = (BasicFileChooserUI)chooser.getUI();
        Action folder = ui.getNewFolderAction();
        folder.setEnabled(false);

        chooser.showSaveDialog(null);
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

Disable the "new folder" Action (which in turn will disable the button):

import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;

public class FileChooserAction
{
    public static void createAndShowUI()
    {
        JFileChooser chooser = new JFileChooser();

        BasicFileChooserUI ui = (BasicFileChooserUI)chooser.getUI();
        Action folder = ui.getNewFolderAction();
        folder.setEnabled(false);

        chooser.showSaveDialog(null);
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}
短叹 2024-11-06 22:29:34

1)这有点愚蠢,但您可以在另一个线程中继续禁用它。直到文件选择器变得不可见。
2)隐藏按钮有用吗? b.setVisible(false);

1) It is a bit stupid, but you can keep disabling it in an another Thread. Until the file chooser got invisible.
2) Does hiding the button work? b.setVisible(false);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文