gpt4 book ai didi

java - 使用按日期排序的文件启动 JFileChooser

转载 作者:搜寻专家 更新时间:2023-11-01 02:12:17 28 4
gpt4 key购买 nike

最近提出的问题:How can I start the JFileChooser in the Details view?answer提供了一个很好的技术来做到这一点。

我想在这里提高一个层次:鉴于我现在知道如何在详细信息 View 中打开 JFileChooser,我是否可以让它打开按日期排序的文件?我知道用户当然可以点击标题,但是有没有办法在代码中实现这一点?

最佳答案

我不知道有任何 API 可以执行此操作。以下代码查找文件选择器使用的表,然后手动对日期列进行排序:

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);

您还需要 Darryl 的 Swing Utils类。

编辑:

显然,一些逻辑在后来的版本中发生了变化,如以下评论中所建议的:

尝试:

JTable table = SwingUtils.getDescendantsOfType(JTable.class, fileChooser).get(0);
table.getModel().addTableModelListener( new TableModelListener()
{
@Override
public void tableChanged(TableModelEvent e)
{
table.getModel().removeTableModelListener(this);
SwingUtilities.invokeLater( () -> table.getRowSorter().toggleSortOrder(3) );
}
});

fileChooser.showOpenDialog(frame);

这会将排序顺序的切换添加到事件调度线程 (EDT) 的末尾,因此它应该在 JTable 详细信息 View 的默认行为之后执行。

关于java - 使用按日期排序的文件启动 JFileChooser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16429779/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com