问题
A recent question asked: How can I start the JFileChooser in the Details view? and the answer provided a nice technique for doing that.
I'd like to raise the aspiration here one level: given that I now know how to open the JFileChooser in details view, can I also get it to open with the files ordered by date? I know the user can of course click on the headings, but is there a way to make this happen in the code?
回答1:
I don't know of any API to do this. The following code finds the table used by the file chooser and then manually does the sort on the date column:
JFrame frame = new JFrame();
JFileChooser fileChooser = new JFileChooser(".");
Action details = fileChooser.getActionMap().get("viewTypeDetails");
details.actionPerformed(null);
// Find the JTable on the file chooser panel and manually do the sort
JTable table = SwingUtils.getDescendantsOfType(JTable.class, fileChooser).get(0);
table.getRowSorter().toggleSortOrder(3);
fileChooser.showOpenDialog(frame);
You will also need Darryl's Swing Utils class.
来源:https://stackoverflow.com/questions/16429779/start-a-jfilechooser-with-files-ordered-by-date