How to pre-populate a JFileChooser will "filename"? - java

I intend to populate a JFileChooser with names from a database but use the standard JFileChooser Dialog for load, delete, save and save-as. I want to give users an impression that they are working on a file system whereas am using a database at the backend to save changes. The user should not be able to browse to a different directory to save or save as. I want to use the same JFileChooser Dialog but with a cancel button and another button(delete|save|save as|load).

JFileChooser chooser = new JFileChooser()
chooser.setSelectedFile(new File("c:/yourPath/someFile") );

Can't be done using the JFileChooser.
JFileChooser only operates on java.io.File's. To do this you would have to subclass java.io.File and create some kind of fake file system that would be very ugly.
You are going to have to make your own save dialog component or find another similar component to use. JFileChooser isnt what you want.

Related

JFileChooser file filtering doesn't actually filter (at least as I intend it to)

I'm trying to set a JFileChooser to only allow choosing a specific file type (pdf) via the showOpenDialog.
I've set a File Filter but I'm confused as to what action on the JFileChooser it has.
What I'm trying to achieve is:
Visually exclude other file types to prevent the user from choosing them from the list.
Actually prevent selection of other types or an invalid file. (i.e. have the getSelectedFile() to actually return a valid pdf file)
Here is my code:
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
fc.setAcceptAllFileFilterUsed(false);
fc.setFileFilter(new FileNameExtensionFilter("PDF Files", "pdf"));
fc.setDialogTitle("Load MSDS");
int op = fc.showOpenDialog(this);
if(op == JFileChooser.APPROVE_OPTION) {
File f = fc.getSelectedFile();
lbl_msds_loaded.setForeground(Color.BLACK);
lbl_msds_loaded.setText(f.getName() + " (Size: " + utils.FileUtils.getFileSizeMegaBytes(f, 3) + ")");
}
I get this behavior:
Visually - The filtering works and the dialog does only show PDF Files, therefore I can only choose pdf files from the list.
But - I'm still able to manually select an invalid file, by typing in some name in the 'File name:' field and click open (or hit enter).
For example: if I write Untitled.png (which does exist in the currently opened directory) and open, I will get that png file loaded.
Or if write a file name that doesn't exist and click open, I will actually get a new file with that name loaded.
(By loaded I mean the file that getSelectedFile() will return).
Is there a way to not allow the dialog approve the open action if an invalid file is set (based on the filter ofcourse)?
Shouldn't this already be the case when using JFileChooser Dialogs with filters?
What exactly is the filter doing here? The documentation for JFileChooser does not explain any of these aspects.
I would really appreciate an explanation on how this works.
Also what is the difference between setFileFilter and addChoosableFileFilter? They give the exact same behavior.
Finally here's a few screenshots of the Dialog and the JFrame form I'm working on for some context:
https://ibb.co/bFVqVmt
https://ibb.co/5BcsXSW
https://ibb.co/2qq0qr9
https://ibb.co/jMXXXyN
https://ibb.co/g3kvtfd
https://ibb.co/2FshJpt
Thanks alot!

Clear file name when enter directory name and enter

I'm working on opening a file using JFileChooser Here is my code
JFileChooser fileChooser = new JFileChooser();
fileChooser.setAcceptAllFileFilterUsed(false);
FileNameExtensionFilter filter = new FileNameExtensionFilter("FF Files", "ff");
fileChooser.addChoosableFileFilter(filter);
int result = fileChooser.showDialog(null, "PP");
on a button click event these code will be running, very normal code I guess. When I click it, the JFileChooser dialog appears. If I enter a directory name in the File Name field (Ex. sam) and hit Enter, it enters to the directory, but the text field still shows the entered text i.e 'sam' I tried the same flow in notepad and in eclipse, in that phase, 'sam' getting cleared so that I can provide another directory name and hit enter.
Correct me if my code is wrong, If this problem is duplicate, I apology for wasted your time.
Notepad and Eclipse use a different implementation than JFileChooser. That´s why it might behave different and I don´t think you can do anything to make it work like you are expecting it (instead of using a custom library or making your own implementation).

How to disable directory opening in JFileChooser? [duplicate]

This question already has answers here:
JFileChooser, want to lock it to one directory
(3 answers)
Closed 9 years ago.
I have a JFileChooser that opens in a specific directory and then allows the user to choose a directory within it (when selected w/ single-click and the OK button is pressed).
However, when the directory is double-clicked, the file chooser opens that directory instead of choosing it.
How can I either
override the double-click to choose the directory
disable navigation outside of the initial directory
disable double-clicking?
I've tried overriding the isTraversable() method in FileView and FileSystemView which works to restrict the file chooser to a directory, however, it then does not show any items inside of said directory.
Here's the code I have right now:
JFileChooser fc = new JFileChooser(dir);
fc.setApproveButtonText("OK");
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fc.setMultiSelectionEnabled(false);
fc.showOpenDialog(fileChooserDialog);
File file = fc.getSelectedFile();
if (file.getParent().equals(dir)) {
//do something
}
You could modify the action map. I don't have a access to a compiler ATM so I can't test this out, but it should work .
JFileChooser chooser = new JFileChooser(".");
ActionMap am = chooser.getActionMap();
Action key = am.get("WHATEVER_THEACTIONAME_FOR_OPEN-DIR._IS") //I think it's "Open Folder";
key.setEnabled(false);
I will update this answer later when I have time and access to a compiler.

How to "Open" and "Save" using 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

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