Java JFilechooser [英] Java JFilechooser

查看:45
本文介绍了Java JFilechooser的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够控制JFileChooser的外观.特别是,我想保存JFileChooser在上次显示时的显示方式.我想保存是否在详细信息/列表视图中使用了它,以及对列表进行了排序的列(例如大小或修改日期).

I would like to be able to control the appearance of the JFileChooser. In particular I would like to save how the JFileChooser was displayed when it was last shown. I would like to save whether it was used in details/list view and which column (eg, size or date modified) the lists were sorted.

我知道关于JFileChooser的很多问题,但是我找不到我想要的东西.

I know there is a lot of questions about JFileChooser but I have not been able to find what I am looking for.

谢谢

,这是一个建议,但保留文件选择器的引用是不够的,因为我想在运行该应用程序的多次过程中保持设置不变

it was suggested as an answer, but keeping a reference of the filechooser is not suffient, as I want to persist the settings across many times that I run the application

例如,我通常要打开我下载的最新文件,因此我想按修改日期排序并在详细视图中显示

for example I usually want to open the most recent file I have downloaded, so I want to sort by date modified and display in detail view

推荐答案

保留对其的引用,仅对其进行一次构造.它应该在随后的情况下打开,看起来非常像用户放置它时的样子.您需要采取额外的步骤来还原文件选择器的位置.

Keep a reference to it and only construct it once. It should open on subsequent occasions looking pretty much like it did when the user disposed it. You would need to take extra steps to restore the location of the file chooser.

在运行之间有多种存储数据的方式(例如,属性文件,XML,Preferences等).这是实现它的快捷方式.

There are a number of ways of storing the data between runs (e.g. a properties file, XML, Preferences etc.). This is the quick'n'dirty way to achieve it.

import java.awt.event.*;
import javax.swing.*;
import java.io.*;

class SerializeMyChooser {

    static private JFileChooser fileChooser;
    static File serializedChooser = new File("chooser.ser");

    public static void main(String[] args) {
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                final JButton showChooser = new JButton("Open File");
                showChooser.addActionListener( new ActionListener() {
                    public void actionPerformed(ActionEvent ae) {
                        if (fileChooser==null) {
                            if (serializedChooser.exists()) {
                                // use the resialized form
                                try {
                                    ObjectInputStream ois = new ObjectInputStream(
                                        new FileInputStream(serializedChooser));
                                    fileChooser = (JFileChooser)ois.readObject();
                                    ois.close();
                                } catch(Exception e) {
                                    // something SNAFU - use fall-back
                                    fileChooser = new JFileChooser();
                                    // configure file chooser..
                                }
                            } else {
                                fileChooser = new JFileChooser();
                                // configure file chooser..
                            }
                        }
                        fileChooser.showOpenDialog(showChooser);
                    }
                });

                JOptionPane.showMessageDialog(null, showChooser);

                if (fileChooser!=null) {
                    try {
                        ObjectOutputStream oos = new ObjectOutputStream(
                            new FileOutputStream(serializedChooser));
                        oos.writeObject(fileChooser);
                        oos.flush();
                        oos.close();
                    } catch(Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }
}

正确的I/O&异常处理留给用户练习.

Correct I/O & exception handling is left as an exercise for the user.

这篇关于Java JFilechooser的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆