Java-从JFileChooser中删除组件(类型为的文件)

Java-从JFileChooser中删除组件(类型为的文件),java,swing,jfilechooser,jcomponent,Java,Swing,Jfilechooser,Jcomponent,如何从JFileChooser中删除组件(类型为的文件);标签和它的组合框 我有以下代码: JFileChooser fileChooser = new JFileChooser(); fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); fileChooser.setDialogTitle("Select Folder"); fileChooser.setApproveButtonText("Select Folder

如何从JFileChooser中删除组件(类型为的文件);标签和它的组合框

我有以下代码:

JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fileChooser.setDialogTitle("Select Folder");
fileChooser.setApproveButtonText("Select Folder");
fileChooser.setAcceptAllFileFilterUsed(false);

hideComponents(fileChooser.getComponents());

private void hideComponents(Component[] components) {

for (int i= 0; i < components.length; i++) {
  if (components[i] instanceof JPanel)
    hideComponents(((JPanel)components[i]).getComponents());
  else if (//component to remove)//what do I check for in here?
    components[i].setVisible(false);
}
JFileChooser fileChooser=newjfilechooser();
fileChooser.setFileSelectionMode(仅限JFileChooser.DIRECTORIES_);
setDialogTitle(“选择文件夹”);
fileChooser.setApproveButtonText(“选择文件夹”);
fileChooser.SetAcceptableFileFilterUsed(false);
hideComponents(fileChooser.getComponents());
私有void隐藏组件(组件[]组件){
对于(int i=0;i
JFileChooser的设计目的不是隐藏其组件。API中没有实现这一点的工具。因为组件是私有的,您无法访问它们,也无法编写代码来隐藏它们

您可能不应该这样做。您可以通过设置“所有文件”过滤器而不设置其他过滤器来禁用控件,在这种情况下,组件将变得不相关


从技术上讲,你可以通过使用反射和违反类保护来做到这一点,但除非它对你的应用程序绝对重要,否则不要这样做。

我尊重地不同意。它有一个功能,我一直都在成功地使用它,特别是与JFileChooser一起使用,尤其是为了让这只被诅咒的野兽同时为这两个应用程序工作DOS和Mac。网络上有很多例子;下面是另一个,从我的工作小程序中挑选出来的。(这个片段还设置了所有组件的背景色)

简言之:原始海报在正确的轨道上-在JFileChooser.getComponents()上迭代。它们不便于识别组件,因此我要做的是查找文本标签,然后获取其所需的祖先。然后,您可以使用Container.getLayout()将其从布局中移除。移除(组件),或者,可以设置可见(false),或者有时可以设置PreferredSize(新维度(0,0))使其消失

// in wrapper:
modifyJChooser(fileChooser.getComponents(), Color.white);

// in component:
private void modifyJChooser(Component[] jc, Color bg) {

    for (int i = 0; i < jc.length; i++) {
        Component c = jc[i];

        // hide file name selection
        if (c.getClass().getSimpleName().equals("MetalFileChooserUI$3")) {
            c.getParent().setVisible(false);
        }

        if (c instanceof JComboBox) {
            Object sel = ((JComboBox) c).getSelectedItem();
            if (sel.toString().contains("AcceptAllFileFilter")) {
                c.setVisible(false);
            }
        } else if (c instanceof JLabel) {
  // **** This is the part that the original poster is looking for ****
            String text = ((JLabel) c).getText();
            if (text.equals("Files of Type:") || text.equals("File Name:") || text.equals("Folder Name:")) {
                c.getParent().getParent().remove(c.getParent());
            }
        } else if (c instanceof JButton) {
            JButton j = (JButton) c;
            String txt = j.getText();
            if (txt != null) {
                if (JCHOOSER_NEW_FOLDER.equalsIgnoreCase(txt)) {
                    j.getParent().setVisible(false); // Disable New Folder on Mac OS
                } else if (JCHOOSER_BTN_CANCEL.equalsIgnoreCase(txt)) {
                    Component parent = c.getParent();
                    ((Container) parent).remove(c);
                }
            }
        }

        if (c instanceof Container)
            modifyJChooser(((Container) c).getComponents(), bg);

        c.setBackground(bg);
    }

}
//在包装器中:
modifyJChooser(fileChooser.getComponents(),Color.white);
//在组件中:
私有void modifyJChooser(组件[]jc,颜色背景){
for(int i=0;i
警告:这在移除的组件曾经所在的位置留下了一点空白。我无法确定其来源;如果有人有线索,请发布

结果是这样的(请注意,我做了代码片段中未显示的其他修改);

由于您违反了大量的良好做法,我将坚持“您可以做到,但您可能不应该做到”。@DJClayworth设置“所有文件”是什么意思过滤器,我还想删除该控件,因为用户无论如何只能选择文件夹,但如果有正确的方法禁用它,那么如果您设置文件过滤器,使其仅显示“所有文件”选项是可用的,这意味着控件没有效果。@DJClayworth好的,我有,我想你的意思是,你实际上可以禁用该控件(灰显),哦,好的。