1
我正在创建一个程序来搜索目录路径。我用JFileChooser来做到这一点很棒。这是它的代码。如何使用JFileChooser添加双反斜杠insead使用JFileChooser
JButton btnPathBrowser = new JButton("Select Database");
btnPathBrowser.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
final JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int response = fc.showOpenDialog(Create.this);
if (response == JFileChooser.APPROVE_OPTION); {
txtPath.setText(fc.getSelectedFile().toString());
//fileName = fc.getSelectedFile().toString();
}
}
});
现在当我运行这个时,我得到了用这种方式写的路径。 GUI showing the Path
所以你可以看到路径被一个反斜杠分开,例如C:\ User \ Folder \ Database,但是我希望它用这样的两个反斜杠分支路径。 C:\ Users \用户数据库。我试过,但得到的错误:
txtPath.setText(fc.getSelectedFile().toString().replace("\", "\\"));
我想用这样的:
String sourceFileName = new String(txtPath.getSelectedText());
我很新的这所以在我的代码正确方向的任何指针将不胜感激。
谢谢,这已经帮了我很多! –