2011-11-29 69 views
3

如何从JFileChooser中删除组件(文件类型);标签和它的组合框?Java - 从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); 
} 

回答

3

我恭敬地不同意。有是它的一个工具,并且我一直都在成功地使用它,特别是使用JFileChooser,特别是使DOS和Mac的诅咒野兽都能工作。网络上有很多例子。这是另一个,从我的工作小程序中挑选出来的。 (这个片段还设置了所有组件的背景颜色)。

总之:原始的海报是在正确的轨道 - 迭代JFileChooser.getComponents()。他们不容易识别组件,因此我所做的是寻找文本标签,然后获得其所需的祖先。然后,您可以使用Container.getLayout(),remove(component),或setVisible(false)从布局中删除它,或者您可以使用setPreferredSize(new Dimension(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); 
    } 

} 

买者:这留下一个位的,其中被去掉的部件一次居住的间隙。我无法确定其来源;如果有人有线索,请发帖。

结果是这样的(注意,我做了其他修改没有显示在代码片段中); enter image description here

+0

既然你违反了一整套良好的做法,我会坚持“你可以做到,但你可能不应该”。 – DJClayworth

0

JFileChooser中没有设计有它的组件隐藏。 API中没有设施来执行此操作。由于这些组件是私人的,因此您无法访问它们,也无法编写代码来自行隐藏它们。

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

从技术上讲,您可以通过使用Reflection来违反类保护措施,但除非它对您的应用程序绝对至关重要,否则不要这样做。

+0

非常感谢您 – jadrijan

+0

@DJClayworth你怎么通过设置“所有文件”过滤器的意思是,我也想删除控制,用户只能选择文件夹过,但如果有一个正确的方式,而不是禁用它那就没事了 –

+0

如果你设置文件过滤器,只有“所有文件”选项可用,那意味着控制没有任何作用。 – DJClayworth