我谨不同意.有一个工具,我一直都成功使用它,特别是与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);
}
}
警告:这留下了一些空白,一旦移除,这些空白就会留在原处.我无法确定其来源;如果有人有线索,请发表.
结果是这样的(请注意,我进行了代码段中未显示的其他修改);