卸载JAVA控件_Java-从JFileChooser删除组件(文件类型)

我谨不同意.有一个工具,我一直都成功使用它,特别是与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);

}

}

警告:这留下了一些空白,一旦移除,这些空白就会留在原处.我无法确定其来源;如果有人有线索,请发表.

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值