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.
Related
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());
// ...
}
I'm trying to use JFileChooser to select a folder. It returns the parent folder, not the current folder.
Example I select folder documents, then backup. JFileChooser returns documents instad of backup.
String getFilePath()
{
JFileChooser fc = new JFileChooser();
// fc.setCurrentDirectory(new File(System.getProperty("user.home")));
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fc.setAcceptAllFileFilterUsed(false);
int returnVal = fc.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File rt=fc.getCurrentDirectory();
String t=rt.getName();
return t;
}
return null;
}
The issue is that you are using JFileChooser.getCurrentDirectory() instead of JFileChooser.getSelectedFile().
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...
while i am trying to save the file its not working but for making folders it works. what should i do ? i am new in java also. plz help
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==save)
{
JFrame parentFrame = new JFrame();
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Specify a file to save");
int userSelection = fileChooser.showSaveDialog(parentFrame);
if (userSelection == JFileChooser.APPROVE_OPTION)
{
File fileToSave = fileChooser.getSelectedFile();
System.out.println("Save as file: " + fileToSave.getAbsolutePath());
}
}
}
You choose a file but don't create it write anything to it. The file will not be created until you actually create it or write something to it, for example with
FileWriter writer = new FileWriter(fileToSave);
writer.write("Hello!");
writer.close();
First, get the file you want to save as a File.
Then, write it to a new directory using BufferedWriter to a new directory.
final JFileChooser fc = new JFileChooser();
int returnVal = fc.showSaveDialog(aComponent); //parent component to JFileChooser
if (returnVal == JFileChooser.APPROVE_OPTION) { //OK button pressed by user
File file = fc.getSelectedFile(); //get File selected by user
o = new BufferedWriter(new FileWriter(file)); //use its name
//write things here
o.flush();
o.close();
}
Look at How to save file using JFileChooser in Java? and How to save a txt file using JFileChooser?
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);