使用JFileChooser返回路径

问题描述 投票:-1回答:1

好吧,我有一个返回路径的方法,由JFileChooser,我想在变量中保存该路径然后修改File。但是当我在JFrame按钮中使用line:tf.guardarTareasHash(operator.obtenerTabla(), "modificar", tf.path());调用该方法时,我意识到我再次打开FileDialog以选择文件。

我想使用tf.path()像参数一样发送,但我没想到会再次打开JFileChooser。线operator.obtenerTabla()发送Hashtable和modificar是String我发送在条件中的cheking如果程序将保存新文件或修改。

    public String path(){
        JFileChooser jfc = new JFileChooser();
        jfc.setCurrentDirectory(new 
        File("C:\\Users\\ARCANET\\Documents\\NetBeansProjects\\hash\\tareas"));
        jfc.showOpenDialog(jfc);
        String ruta = jfc.getSelectedFile().getAbsolutePath();        
        return ruta;
}

¿无论如何存储所选文件的路径而不再打开OpenDialog?我想为它做一个static变量。

java jfilechooser
1个回答
0
投票

如果我理解你想要的话

tf.guardarTareasHash(operator.obtenerTabla(), "modificar", tf.path());

不要再次打开文件对话框。在这种情况下,您需要在第一次调用路径方法时存储路径方法的结果,然后将结果作为第三个参数传递,而不是再次调用路径方法。

class MyClass {
    String myPath = null;
    ...
    // call the path method which opens the file dialog
    myPath = path();
    ...
    // use the saved result
    tf.guardarTareasHash(operator.obtenerTabla(), "modificar", myPath);
}

如果myPath未初始化(例如,用户取消文件对话框),您仍需要执行检查操作

© www.soinside.com 2019 - 2024. All rights reserved.