How to "Open" and "Save" using java - java

I want to make an "Open" and "Save" dialog in java. An example of what I want is in the images below:
Open:
Save:
How would I go about doing this?

You want to use a JFileChooser object. It will open and be modal, and block in the thread that opened it until you choose a file.
Open:
JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showOpenDialog(modalToComponent) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
// load from file
}
Save:
JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showSaveDialog(modalToComponent) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
// save to file
}
There are more options you can set to set the file name extension filter, or the current directory. See the API for the javax.swing.JFileChooser for details. There is also a page for "How to Use File Choosers" on Oracle's site:
http://download.oracle.com/javase/tutorial/uiswing/components/filechooser.html

I would suggest looking into javax.swing.JFileChooser
Here is a site with some examples in using as both 'Open' and 'Save'. http://www.java2s.com/Code/Java/Swing-JFC/DemonstrationofFiledialogboxes.htm
This will be much less work than implementing for yourself.

Maybe you could take a look at JFileChooser, which allow you to use native dialogs in one line of code.

You can find an introduction to file dialogs in the Java Tutorials. Java2s also has some example code.

First off, you'll want to go through Oracle's tutorial to learn how to do basic I/O in Java.
After that, you will want to look at the tutorial on how to use a file chooser.

You may also want to consider the possibility of using SWT (another Java GUI library). Pros and cons of each are listed at:
Java Desktop application: SWT vs. Swing

Related

how to limit the pre-loading of files in Java (JFileChooser)

I have a Java program to search and load Excel files from the PC to use it in a SAS proyect. Usually works well but when I install it in a server with thousands of folders it takes too long and I would like to do it in seconds, not in 15 minutes.
I think is because JFileChooser try to load all directories and disk (the server has many disks). So I'd like to limit the search to the disk in witch I work. My programme is such that:
private String createProyect() {
JFileChooser fileopen = new JFileChooser();
FileFilter filter = new FileNameExtensionFilter(NEW_PROYECT, EXTENSION_XLS, EXTENSION_XLSX);
fileopen.addChoosableFileFilter(filter);
...}
Maybe I need to use a configuration file but I don't know how.
Perhaps you are looking for FileSystemView ?
If you use it, you will be able to modify the scope of your JFileChooser, like this example shows: Example

How can I open a file from the explorer and copy to a directory?

I have a little problem,
I need to do a program that open the file explorer and copy the selected file in a specified directory.
I've only found how to open the explorer :
File file = new File ("c:\\<directory>");
Desktop desktop = Desktop.getDesktop();
desktop.open(file);
but I need to get the selected file and copy in a default directory, and I really don't know how to do it.
Thanks a lot!!
I assume you have a SWT Java Application. Then you can use FileDialog to show a file chooser where the user selects a file. Without a GUI it wouldn't be easy to show a file choosing dialog to the user.
This code snippet uses FileUtils from Apache Commons IO:
FileDialog dlg = new FileDialog(frame, "Choose a file to copy", FileDialog.OPEN);
dlg.setVisible(true); // blocks until user completes the action
String fileToCopyString = dlg.getFile();
if (fileToCopyString != null) {
File fileToCopy = new File(fileToCopyString);
if (fileToCopy.isFile()) {
FileUtils.copyFile(fileToCopy, new File(tmpDir, fileToCopy.getName());
}
}
References:
FileDialog Javadoc
FileUtils Javadoc
How to use FileDialog?
Try read the file and write it to another directory. You can use FileReader() and FileWriter() methods.

how to show all files of server using jfilechooser in java?

I want show all files of server using jfilechooser and choose any file then download that.
But i don't know that, is it possible? Please give me some suggestion?
It's not easy but I think it's possible. You need to set a custom FileSystemView JFileChooser.setFileSystemView. Try to use google search (filesystemview ftp). Probably you can find implementation of FileSystemView which supports FTP.
You can make use of FileNameExtensionFilter. Using it you can add extensions for selection.
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("jpg", "gif");
chooser.setFileFilter(filter);

How to browse folders in java?

I tried to create a JFileChooser but I don't understand how to set it to show directories only.
JFileChooser f = new JFileChooser();
f.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if(f.showOpenDialog(parent)== JFileChooser.APPROVE_OPTION) {
File result = f.getSelectedFile();
} else {
...
}
Have look at this code snippet - that sounds promising.
The most intersting line is:
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
Apart from what Michael already suggested you might take a look at JIDE OSS, a free Swing components library, which amongst many other goodies provides a much nicer folder chooser component(FolderChooser).

Read / write program in Java using JFileChooser

How would I link the file choosen from a JFileChooser to a file and how would I convert it to string being able to display and edit it in a TextArea?
I have the GUI set up using swing, but the link between actionListener and the JFileChooser is not complete.
Any help would be much appreciated.
Code: http://pastebin.com/p3fb17Wi
EDIT: I found this program, that does pretty much what i wanted to, but it does not allow me to save the actual file : http://www.java-forums.org/new-java/8856-how-get-content-text-file-write-jtextarea.html
To be able to save the changes you have made, you will have to use a Save Dialog. In the example you have quoted, a File Open Dialog is used. They work in a similar way, all that you need to do is then get the file to which the user would like to store the changes made, open a stream to it and write the data back. This tutorial shows you how to use the various File Choosers.
All text components support a read(...) and write(...) method. So all you have to do is get the name of the File and create your FileReader or FileWriter and then invoke the method.
All the file chooser is used for is the get the File name to be used by the reader or writer. So the basic code would be:
File saveFile = chooser.getSelectedFile();
FileWriterr writerr = new FileWriter( saveFile );
textArea.write(writer)
Of course you will probably want to use a Buffered reader/writer.

Categories

Resources