TITLE:JFileChooserのデフォルトをDetailsViewに設定
#navi(../)
RIGHT:Posted by [[terai]] at 2011-01-10
*JFileChooserのデフォルトをDetailsViewに設定 [#f6a8221b]
JFileChooserを開いたときのデフォルトをリストではなく詳細に変更します。

//-&jnlp;
-&jar;
-&zip;

//#screenshot
#ref(http://lh3.ggpht.com/_9Z4BYR88imo/TSq77M-soeI/AAAAAAAAAxg/0nnen-n-cAY/s800/DetailsViewFileChooser.png)

**サンプルコード [#bb304f2a]
#code{{
searchAndClick(chooser, UIManager.getIcon("FileChooser.detailsViewIcon"));
}}
#code{{
private static boolean searchAndClick(Container parent, Icon icon) {
  for(Component c:parent.getComponents()) {
    if(c instanceof JToggleButton && ((JToggleButton)c).getIcon()==icon) {
      ((AbstractButton)c).doClick();
      return true;
    }else{
      if(searchAndClick((Container)c, icon)) return true;
    }
  }
  return false;
}
}}

**解説 [#d6cd620d]
上記のサンプルでは、JFileChooserの子で、UIManager.getIcon("FileChooser.detailsViewIcon")アイコンが設定されているJToggleButtonを検索し、doClick()することで、ListからDetailsView(詳細)に切り替えています。

----
警告されますが、以下のように sun.swing.FilePane#setViewType(sun.swing.FilePane.VIEWTYPE_DETAILS)を使用する方法もあります。
#code{{
sun.swing.FilePane filePane = (sun.swing.FilePane)findChildComponent(chooser, sun.swing.FilePane.class);
filePane.setViewType(sun.swing.FilePane.VIEWTYPE_DETAILS);
}}


**参考リンク [#k11fb09b]
**コメント [#k2202d2b]
#comment