So I wanted to save files needed for the program, but the user has to decide where to save files... But because I dont want the user to select the path everytime he starts the program, the program should save the path where to go to find the files, how to?
I made the user select the file with a JFileChooser.
JButton jButton = new JButton();
JFileChooser jFileChooser = new JFileChooser();
jFileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
jFileChooser.setDialogTitle("Choose your Path!");
jFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if(jFileChooser.showOpenDialog(jButton) == JFileChooser.APPROVE_OPTION){
}
path = jFileChooser.getSelectedFile().getAbsolutePath();
pathBind = jFileChooser.getSelectedFile().getAbsolutePath() + "/binds.jar";
bindFile = new File(pathBind);
If any further information is needed, I'll give it... Sorry if I forgot something^^.
That kind of information probably belongs in user preferences, which you access using the Preferences class:
private static final String LAST_SAVE_DIR_PREFS_KEY = "last-save-dir";
private static final Preferences PREFERENCES =
Preferences.userNodeForPackage(MyUserInterface.class);
// ...
String saveDir = PREFERENCES.get(LAST_SAVE_DIR_PREFS_KEY,
System.getProperty("user.home"));
jFileChooser.setSelectedFile(new File(saveDir));
jFileChooser.setDialogTitle("Choose your Path!");
jFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (jFileChooser.showOpenDialog(jButton) == JFileChooser.APPROVE_OPTION) {
PREFERENCES.put(LAST_SAVE_DIR_PREFS_KEY,
jFileChooser.getSelectedFile().toString());
// ...
}
Related
I have convert file into DBF format. But i must save that dbf file into specific folder which has generated when i create directory. The common coding just written like this
import java.io.File;
// demonstrates how to create a directory in java
public class JavaCreateDirectoryExample
{
public static void main(String[] args)
{
File dir = new File("/Users/al/tmp/TestDirectory");
// attempt to create the directory here
boolean successful = dir.mkdir();
if (successful)
{
// creating the directory succeeded
System.out.println("directory was created successfully");
}
else
{
// creating the directory failed
System.out.println("failed trying to create the directory");
}
}
}
But, i would like change "/Users/al/tmp/TestDirectory" into a dynamic state which i take it from the path JFileChooser that i've made. Is there any possibilities to make it done? Thanks a lot
If you want to use the same variable, you can instantiate it again.
File dir = new File("/Users/al/tmp/TestDirectory");
boolean successful = dir.mkdir();
// Here we assign a new value by calling the constructor
dir = new File("/Users/al/tmp/AnotherTestDirectory");
// Next we create the new directory using the same method
boolean successful2 = dir.mkdir();
I'm guessing your quite new to Java. You should get in the habit of reading the API. It is your friend and will help answer these questions.
The JFileChooser can be used to get the selected File which can be used to get the path
JFileChooser API:
getSelectedFile() - Returns the selected file.
File API:
getAbsolutePath() - Returns the absolute pathname string of this abstract pathname.
...and some sample code
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
String path = chooser.getSelectedFile().getAbsolutePath();
File dir = new File(path);
....
}
I have several dialog boxes which provide a File Chooser. For the first, my coding was like this
JFileChooser chooser= new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal= chooser.showOpenDialog(this);
if(returnVal==JFileChooser.APPROVE_OPTION){
File f= chooser.getSelectedFile();
jTextField1.setText(f.getPath());
chooser.setCurrentDirectory(f);
}
In my case, i would like to set the last path which is selected as the default path in next selection JFileChooser. Is there any solution for me?
Thanks for any response
Depending on your requirements, you can use Preferences to store it away and use it again after the program has been restarted.
Preferences pref = Preferences.userRoot();
// Retrieve the selected path or use
// an empty string if no path has
// previously been selected
String path = pref.get("DEFAULT_PATH", "");
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
// Set the path that was saved in preferences
chooser.setCurrentDirectory(new File(path));
int returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
File f = chooser.getSelectedFile();
chooser.setCurrentDirectory(f);
// Save the selected path
pref.put("DEFAULT_PATH", f.getAbsolutePath());
}
You will have to "remember" the last path.
This can easily be done by storing the value in a instance variable...
private File lastPath;
//...
lastPath = f.getParentFile();
And simply resetting it when you need to...
//...
if (lastPath != null) {
chooser.setCurrentDirectory(lastPath);
}
You could also use a single instance of the JFileChooser, so each time you show it, it will be at the last location it was used...
export content to an xls file using java with save dialog:
- Where should I ask for a file name during save?
I prefer to ask what to name the file when it is saved.
public static void main(String args[])
{
SaveToExcel exp = new SaveToExcel();
exp.writePropertiesIntoExcel("D:\\ExcelFile.xls");
}
You should use JFileChooser if you want to let the users choose where to save the file.
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.APPROVE_OPTION);
chooser.setMultiSelectionEnabled(false);
int returnVal = chooser.showSaveDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION){
File f = chooser.getSelectedFile();
String a = f.getAbsolutePath();
SaveToExcel exp = new SaveToExcel();
exp.writePropertiesIntoExcel(a);
}
A simple code you can use in your method, this gets the path you want to save your file in.
I have two seperate methods of opening a file.
The first uses a FileChoser with an additional file type filter.
JFileChooser inFileName = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("PCF & TXT Files", "pcf", "txt");
inFileName.setFileFilter(filter);
Component parent = null;
int returnVal = inFileName.showOpenDialog(parent);`
The second uses a JOptionPane but has a loop to ensure the directory chosen exists
String filePath;
File directory;
do{
filePath = JOptionPane.showInputDialog("please enter directory");
directory = new File(filePath);
if (directory.exists()==false){
JOptionPane.showMessageDialog(null,"error with directory");
}
}while(directory.exists()==false);
I'm looking to get the best of both here. To be able to choose a file, using a file filter and also loop that function should that directory not be valid.
I've tried switching around variable names and the various functions in different places but I cant seem to get the loop (".exists" function) to work.
You just need to modify your JFileChooser code to use a loop.
JFileChooser inFileName = new JFileChooser();
File file;
boolean valid = false;
while (!valid) {
int returnVal = inFileName.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = inFileName.getSelectedFile();
valid = file.isDirectory();
else {
valid = returnVal == JFileChooser.CANCEL_OPTION;
}
}
Its worth mentioning that this kind of thing might be better achieved using;
jFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
I current have a JFrame application that uses JTextField's for input of file location as per the code below.
txtSource = new JTextField();
txtSource.setToolTipText("/location/of/file/test.txt");
txtSource.setText("/location/of/file/test.txt");
txtSource.setBounds(16, 122, 412, 29);
contentPane.add(txtSource);
txtSource.setColumns(10);
What I would like to do is allow a directory search for the user to select the location of the file on the local computer and that location would populate into the Textbox.
I found the below information on JCHooser, but I'm not sure if this is way to go and would like assistance on how to implement.
String filename = File.separator+"tmp";
JFileChooser fc = new JFileChooser(new File(filename));
// Show open dialog; this method does not return until the dialog is closed
fc.showOpenDialog(frame);
File selFile = fc.getSelectedFile();
// Show save dialog; this method does not return until the dialog is closed
fc.showSaveDialog(frame);
selFile = fc.getSelectedFile();
Thanks in advance
Use
int option = fc.showOpenDialog(frame);
if (option == JFileChooser.APPROVE_OPTION) {
txtSource.setText(fc.getSelectedFile().getAbsolutePath())
}
populates the text field with the selected file absolute file location