我恭敬地不同意。有是是它的一个工具,并且我一直都在成功地使用它,特别是使用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);
}
}
买者:这留下一个位的,其中被去掉的部件一次居住的间隙。我无法确定其来源;如果有人有线索,请发帖。
结果是这样的(注意,我做了其他修改没有显示在代码片段中);
既然你违反了一整套良好的做法,我会坚持“你可以做到,但你可能不应该”。 – DJClayworth