私は敬意を表して反対します。そのための機能があり、特にJFileChooserで、特に呪われた獣をDOSとMacの両方で機能させるために、私は常にそれをうまく使用しています。Webには多数の例があります。これは、私の作業アプレットから抜粋した別のアプレットです。(このスニペットは、すべてのコンポーネントの背景色も設定します)。
つまり、元のポスターは正しい方向に進んでいました。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);
}
}
警告:これにより、削除されたコンポーネントがかつて存在していた場所に少しギャップが残ります。私はその出所を特定することができませんでした。手がかりがあれば投稿してください。
結果は次のようになります(コードスニペットに表示されていない他の変更を行うことに注意してください)。
