java - 使用 JFileChooser 返回路径
问题描述
好吧,我有一个返回路径的方法,通过JFileChooser
,我想将该路径保存在一个变量中,然后修改一个File
. JFrame
但是当我用 line:调用按钮中的方法时 tf.guardarTareasHash(operator.obtenerTabla(), "modificar", tf.path());
,我意识到我FileDialog
再次打开以选择文件。
本来想用tf.path()
send之类的参数,但没想到会再次打开JFileChooser。如果程序将保存新文件或修改,该行发送一个和修饰符是我在条件中发送用于检查operator.obtenerTabla()
的Hashtable
一个。String
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
如果未初始化,您仍然必须执行检查(例如用户取消文件对话框)
推荐阅读
- java - @ManyToOne @OneToMany 映射,外键为空
- python - 我们可以在不显式覆盖的情况下使用 python 关键字作为变量名吗?
- java - 无法使用 Java Swing 添加图像
- python - 使用 enumerate 标记绘图上的点,每个点使用不同的字母
- automation - 如何使用 Vividus 工具测试下载文件
- java - new File(String url, String child) 修改 url
- ansible - 在多个主机名上运行时,使用什么代替 lineinfile 通过 ansible 打印文件中的重复值
- ios - UITableViewCell如何添加带圆角的实心边框
- javascript - 如果所有三个容器都使用一种方法呈现,我如何使所有三个容器在 Dragula 中可拖动?使用 React js 和 Dragula(不是 react-dragula)
- visual-studio-code - 为什么文件保存快捷方式(ctrl + s)在 Visual Studio 代码中不起作用?