理想的な解決策は、JFileChooser クラスのどこかに構成値を設定して、シングルクリックまたはダブルクリック モードで機能するようにすることです。
そのような構成はないように思われるので、Richie_W のアイデアに基づくおおよその解決策を次に示します。ユーザーが多くのディレクトリをナビゲートできるようにし、選択を設定するときに発生する再入可能イベントを回避するために、少し拡張する必要がありました。ただし、Oscar が指摘したように、キーボードを使用してナビゲートすることはできません (フォーカスされているものは常に選択されます)。キーボードを使用しない場合は、機能します。
JFileChooser _fileChooser=new JFileChooser();
if (ConfigurationManager.isSingleClickDesired()) {
//We will be interested in files only, but we need to allow it to choose both
_fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
_fileChooser.addPropertyChangeListener(new PropertyChangeListener() {
//To prevent reentry
private boolean handlingEvent=false;
public void propertyChange(PropertyChangeEvent e) {
//Prevent reentry
if (handlingEvent)
return;
else
//Mark it as handling the event
handlingEvent=true;
String propertyName = e.getPropertyName();
//We are interested in both event types
if(propertyName.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY) ||
propertyName.equals(JFileChooser.DIRECTORY_CHANGED_PROPERTY)){
File selectedFile = (File) e.getNewValue();
if (selectedFile!=null) {
if (selectedFile.isDirectory()) {
//Allow the user to navigate directories with single click
_fileChooser.setCurrentDirectory(selectedFile);
} else {
_fileChooser.setSelectedFile(selectedFile);
if (_fileChooser.getSelectedFile()!=null)
//Accept it
_fileChooser.approveSelection();
}
}
}
//Allow new events to be processed now
handlingEvent=false;
}
});
}
Ps->見栄えの悪いコード形式で申し訳ありませんが、StackoverFlow は、KDE および Gnome での Firefox および Iceweasel コード形式のサポートを壊しています。