好吧,我有一个返回路径的方法,由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
变量。
如果我理解你想要的话
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
未初始化(例如,用户取消文件对话框),您仍需要执行检查操作