Start a JFileChooser with files ordered by date

☆樱花仙子☆ 提交于 2019-12-07 07:58:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!