Swing/DetailsViewFileChooser のバックアップの現在との差分(No.7)
- category: swing folder: DetailsViewFileChooser title: JFileChooserのデフォルトをDetails Viewに設定 tags: [JFileChooser, FilePane] author: aterai pubdate: 2011-01-10T17:02:55+09:00 description: JFileChooserを開いたときのデフォルトをリストではなく詳細に変更します。 image:
概要
概要
JFileChooser
を開いたときのデフォルトをリストではなく詳細に変更します。
Screenshot
Advertisement
サンプルコード
サンプルコード
#spandel
//java - How can I start the JFileChooser in the Details view? - Stack Overflow]
#spanend
#spandel
//http://stackoverflow.com/questions/16292502/how-can-i-start-the-jfilechooser-in-the-details-view
#spanend
#spandel
//for(Object key: chooser.getActionMap().allKeys()) {
#spanend
#spandel
// System.out.println(key);
#spanend
#spandel
//}
#spanend
#spandel
Action detailsAction = chooser.getActionMap().get("viewTypeDetails");
#spanend
#spandel
if(detailsAction!=null) detailsAction.actionPerformed(null);
#spanend
#spanadd
// java - How can I start the JFileChooser in the Details view? - Stack Overflow]
#spanend
#spanadd
// https://stackoverflow.com/questions/16292502/how-can-i-start-the-jfilechooser-in-the-details-view
#spanend
#spanadd
// for (Object key: chooser.getActionMap().allKeys()) {
#spanend
#spanadd
// System.out.println(key);
#spanend
#spanadd
// }
#spanend
#spanadd
String cmd = "viewTypeDetails";
#spanend
#spanadd
Optional.ofNullable(chooser.getActionMap().get(cmd))
#spanend
.ifPresent(a -> a.actionPerformed(new ActionEvent(e.getSource(), e.getID(), cmd)));
View in GitHub: Java, Kotlin解説
- 以下のリンクで紹介されているように、
ActionMap
からviewTypeDetails
アクションを取得する方法が一番簡単なようです。- java - How can I start the JFileChooser in the Details view? - Stack Overflow
- 何時から使用できるようになったのか、それとも元から使用可能だったのかなどを調査中。
解説
- java - How can I start the JFileChooser in the Details view? - Stack Overflowで紹介されている
ActionMap
からviewTypeDetails
アクションを取得してDetailsView
(詳細)に切り替え
- -
JFileChooser
の子でUIManager.getIcon("FileChooser.detailsViewIcon")
アイコンが設定されているJToggleButton
を検索、doClick()
することで、List
からDetailsView
(詳細)に切り替える方法もあります。#spanend #spanadd // @see javax/swing/plaf/basic/BasicFileChooserUI.java #spanend #spanadd ActionMap map = new ActionMapUIResource(); #spanend #spanadd Action refreshAction = new UIAction(FilePane.ACTION_REFRESH) { #spanend public void actionPerformed(ActionEvent e) { getFileChooser().rescanCurrentDirectory(); } #spanadd }; #spanend #spanadd map.put(FilePane.ACTION_APPROVE_SELECTION, getApproveSelectionAction()); #spanend #spanadd map.put(FilePane.ACTION_CANCEL, getCancelSelectionAction()); #spanend #spanadd map.put(FilePane.ACTION_REFRESH, refreshAction); #spanend #spanadd map.put(FilePane.ACTION_CHANGE_TO_PARENT_DIRECTORY, getChangeToParentDirectoryAction()); #spanend #spanadd // sun.swing.FilePane.ACTION_APPROVE_SELECTION = "approveSelection"; #spanend #spanadd // sun.swing.FilePane.ACTION_CANCEL = "cancelSelection"; #spanend #spanadd // sun.swing.FilePane.ACTION_EDIT_FILE_NAME = "editFileName"; #spanend #spanadd // sun.swing.FilePane.ACTION_REFRESH = "refresh"; #spanend #spanadd // sun.swing.FilePane.ACTION_CHANGE_TO_PARENT_DIRECTORY = "Go Up"; #spanend #spanadd // sun.swing.FilePane.ACTION_NEW_FOLDER = "New Folder"; #spanend #spanadd // sun.swing.FilePane.ACTION_VIEW_LIST = "viewTypeList"; #spanend #spanadd // sun.swing.FilePane.ACTION_VIEW_DETAILS = "viewTypeDetails"; #spanend #spanadd
-
MetalLookAndFeel
などのJFileChooser
の子でUIManager.getIcon("FileChooser.detailsViewIcon")
アイコンが設定されているJToggleButton
を検索、doClick()
することでList
からDetailsView
(詳細)に切り替え-
WindowsLookAndFeel
でのJFileChooser
のDetailsView
切り替えはJRadioButtonMenuItem
なのでこの方法は使用不可
-
#spandel
//searchAndClick(chooser, UIManager.getIcon("FileChooser.detailsViewIcon"));
#spanend
#spanadd
// searchAndClick(chooser, UIManager.getIcon("FileChooser.detailsViewIcon"));
#spanend
private static boolean searchAndClick(Container parent, Icon icon) {
for(Component c:parent.getComponents()) {
if(c instanceof JToggleButton && ((JToggleButton)c).getIcon()==icon) {
((AbstractButton)c).doClick();
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;
} else {
if (searchAndClick((Container) c, icon)) {
return true;
}
}
}
return false;
}
- -
警告されますが、以下のように
sun.swing.FilePane#setViewType(sun.swing.FilePane.VIEWTYPE_DETAILS)
メソッドを使用する方法もあります。
- 警告されるが以下のように
sun.swing.FilePane#setViewType(sun.swing.FilePane.VIEWTYPE_DETAILS)
メソッドを使用する方法もある
#spandel
FilePane filePane = (FilePane)findChildComponent(chooser, FilePane.class);
#spanend
#spanadd
FilePane filePane = (FilePane) findChildComponent(chooser, FilePane.class);
#spanend
filePane.setViewType(FilePane.VIEWTYPE_DETAILS);
参考リンク
- java - How can I start the JFileChooser in the Details view? - Stack Overflow
- Swing Utils « Java Tips Weblog
参考リンク
- java - How can I start the JFileChooser in the Details view? - Stack Overflow
- Swing Utils « Java Tips Weblog
- sun.swing: FilePane.java