I want to change JFileChooser
start directory to desktop. So, in my computer I wrote:
JFileChooser fc = new JFileChooser("C:\\Users\\LNK\\Desktop");
The problem is, when I compile my code and run program in another computer it doesn't work because there are no C:\\Users\\LNK\\Desktop
path. So, is there some kind of "apsolute" path of desktop?
The object of JFileChooser class represents a dialog window from which the user can select file.
If you want to save and reconstruct a JFrame, you'll need to save the data being used (in the labels and boxes) and write code to parse and reconstruct from the saved data. This is why you should build the logic model for your application before adding a GUI to it.
You can use a user.home
system property to get user directory.
So your code would look like
String userDir = System.getProperty("user.home");
JFileChooser fc = new JFileChooser(userDir +"/Desktop");
Here is another option for getting to the Windows desktop:
fileChooser.setCurrentDirectory(new File("C:\\"));
Action details = fileChooser.getActionMap().get("Go Up");
details.actionPerformed(null);
details.actionPerformed(null);
If you leave off the last line, you will get to "Computer"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With