gpt4 book ai didi

java - 如何在 jfilechooser 中关闭 "create new folder"的可见性

转载 作者:行者123 更新时间:2023-11-29 05:18:43 25 4
gpt4 key购买 nike

我的目标是关闭 file chooser 上的“创建新文件夹”按钮.是否可以在文件选择器上设置“创建新文件夹”按钮的可见性?我可以将以“查找”字样开头的行的第一个组件的可见性设置为关闭,但我只想设置“创建新文件夹”而不是所有组件的可见性。我该怎么做?

最佳答案

两个建议:

您可以通过访问默认操作并禁用操作来禁用按钮:

Action folder = fileChooser.getActionMap().get("New Folder");
folder.setEnabled( false );

或者您可以使用反射来访问按钮并使按钮不可见:

//JButton button = SwingUtils.getDescendantOfType(
// JButton.class, fileChooser, "ToolTipText", "Create New Folder");
//button.setVisible(false);

对于这种方法,您需要使用 Swing Utils类。

这是一个展示这两种方法的快速演示:

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

public class Main
{
public static void main(String[] args)
{
JFileChooser fileChooser = new JFileChooser();

Action folder = fileChooser.getActionMap().get("New Folder");
folder.setEnabled( false );

// Make the button invisible

//JButton button = SwingUtils.getDescendantOfType(
// JButton.class, fileChooser, "ToolTipText", "Create New Folder");
//button.setVisible(false);

fileChooser.showOpenDialog(null);
}
}

关于java - 如何在 jfilechooser 中关闭 "create new folder"的可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25592615/

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