Swing/FileHidingEnabled のバックアップ差分(No.5)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/FileHidingEnabled へ行く。
- 追加された行はこの色です。
- 削除された行はこの色です。
--- category: swing folder: FileHidingEnabled title: JFileChooserでの隠しファイルの非表示設定を変更する tags: [JFileChooser, JPopupMenu] author: aterai pubdate: 2014-03-17T00:01:02+09:00 description: JFileChooserで隠しファイルを表示するかどうかをポップアップメニューから切り替えます。 image: https://lh4.googleusercontent.com/-TSMPljQ02Ao/UyWixahVFzI/AAAAAAAACBw/n_Ctee0FJGQ/s800/FileHidingEnabled.png --- * 概要 [#summary] `JFileChooser`で隠しファイルを表示するかどうかをポップアップメニューから切り替えます。 #download(https://lh4.googleusercontent.com/-TSMPljQ02Ao/UyWixahVFzI/AAAAAAAACBw/n_Ctee0FJGQ/s800/FileHidingEnabled.png) * サンプルコード [#sourcecode] #code(link){{ chooser = new JFileChooser(); JPopupMenu pop = searchPopupMenu(chooser); pop.addSeparator(); JCheckBoxMenuItem mi = new JCheckBoxMenuItem(new AbstractAction("isFileHidingEnabled") { @Override public void actionPerformed(ActionEvent e) { chooser.setFileHidingEnabled(((JCheckBoxMenuItem) e.getSource()).isSelected()); } }); mi.setSelected(chooser.isFileHidingEnabled()); pop.add(mi); }} * 解説 [#explanation] 上記のサンプルでは、`JFileChooser#setFileHidingEnabled(boolean)`メソッドを使用して、隠しファイル、隠しフォルダーなどの表示・非表示を設定しています。 ---- - `JFileChooser`からファイルリストの`JPopupMenu`を直接取得することが出来ないので、子コンポーネントを検索する必要がある -- 参考: [[Containerの子Componentを再帰的にすべて取得する>Swing/GetComponentsRecursively]] - 初期値は、`OS`の設定(`Windows`なら「コントロールパネル、フォルダーオプション、表示、ファイルとフォルダーの表示」)に従う -- 参考: [[DesktopPropertyの変更を監視する>Swing/DesktopProperty]] #code{{ Object showHiddenProperty = Toolkit.getDefaultToolkit().getDesktopProperty("awt.file.showHiddenFiles"); System.out.println("awt.file.showHiddenFiles: " + showHiddenProperty); }} ---- `JFileChooser`から`JPopupMenu`を直接取得することが出来ないので、以下のように子コンポーネントを検索しています。 #code{{ private static JPopupMenu searchPopupMenu(Container parent) { for (Component c: parent.getComponents()) { if (c instanceof JComponent && ((JComponent) c).getComponentPopupMenu() != null) { return ((JComponent) c).getComponentPopupMenu(); } else { JPopupMenu pop = searchPopupMenu((Container) c); if (pop != null) { return pop; } } } return null; } }} * 参考リンク [#reference] - [http://crocro.com/news/20110706140746.html クロノス・クラウン - 「JFileChooser」のコンテキストメニューに独自メニューを追加する方法] - [[DesktopPropertyの変更を監視する>Swing/DesktopProperty]] - [[Containerの子Componentを再帰的にすべて取得する>Swing/GetComponentsRecursively]] * コメント [#comment] #comment #comment