问题描述
如何从JFileChooser中删除组件(文件类型);标签和它的组合框?
How can I remove component (Files of Type) from JFileChooser; both label and its combobox?
我有以下代码:
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一起使用时,尤其是使被诅咒的野兽在DOS和Mac上都可以使用.网上有很多例子.这是从我工作的小程序中剔除的另一个. (此代码段还设置了所有组件的背景颜色.)
I respectfully disagree. There is a facility for it, and I use it successfully all the time, particularly with the JFileChooser and particularly to make the cursed beast work for both DOS and Mac. There are numerous examples on the web; here is another, culled from my working applet. (This snippet also sets the background color on all components).
简而言之:原始海报位于正确的轨道上-遍历JFileChooser.getComponents().它们使识别组件变得不容易,所以我要做的是寻找文本标签,然后获取其所需的祖先.然后,您可以使用Container.getLayout().remove(component)将其从布局中删除,或者可以设置setVisible(false),或者有时可以setPreferredSize(new Dimension(0,0))使其消失. /p>
In short: The original poster was on the right track - iterate over JFileChooser.getComponents(). They don't make it easy to identify a component, so what I do is look for a text label and then get its desired ancestor. You can then remove that from the layout using Container.getLayout().remove(component), or, you can setVisible(false), or you can sometimes setPreferredSize(new Dimension(0,0)) to make it go away.
// 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);
}
}
注意事项:这会在删除的组件曾经驻留的地方留出一些空隙.我无法确定其来源;如果有人有线索,请发表.
Caveat: This leaves a bit of a gap where the removed components once resided. I have not been able to identify its source; if anybody has a clue, please post.
结果是这样的(请注意,我进行了代码段中未显示的其他修改);
Result is like this (note that I make other modifications not shown in code snippet);
这篇关于Java-从JFileChooser删除组件(文件类型)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!