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).
Related
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!
I created a mp3 player in java and then created it as a .exe file. Now to play song in mp3 player I have to choose song in JFileChooser then it plays that song.
if (chooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
Path=chooser.getSelectedFile().getAbsolutePath().replace('\\','/');
But I want that when I double click on that song in Windows explorer or in My Computer it will play that song directly like VLC or other player do. We don't need to first choose the song in a file chooser, we just need to click on a song and it play.
Adding to Raymond Holguin's answer in your main method save the first parameter and if its a File that exists then send it to your player class and set to start playing (not show UI with play button)
public static void main(String args){
if(args.length > 0){
java.io.File possibleAudioClipFile = new File(args[0]);
if(possibleAudioClipFile.exists() && possibleAudioClipFile.isFile.isFile() && possibleAudioClipFile.canRead()){
/might want to check extn or first few bytes then play or show error msg and exit/ show default UI
}
}
besides you need to make sure each customer/user has your app assocaited with the audio file types by extention name . Can use a script in command propmt using assoc http://support.microsoft.com/kb/323526 or in windows explorer Tools/Options menu.
This is a windows config issue, and possibly an application issue.
1) In windows you need to make the association with .mp3 files that says whenever an MP3 is opened to use your program. If you right click on an MP3 -> Open With you can choose the defalut program for that file type
2) In your program you need to handle the file information that is going to be passed into your application in order to open it. So instead of using File info from JChooseer your going to use the File info that is passed as an input into your application.
You need to associate your application with the filetypes you support. On Windows, this means creating an association to a URI scheme in the registry. See here: https://msdn.microsoft.com/en-us/library/ie/aa767914%28v=vs.85%29.aspx
This is what happens when you right-click and choose what executable to open a particular file type with.
I am currently opening the file like this:
File f = File.createTempFile(prefix, suffix);
f = FileUtil.writeBytes(b.getBlob(), f);
Desktop.getDesktop().open(f);
Context description :
The file is stored on a server. When you want to view the file the byte[] is retrieved from the server and is put in a File object (see line 2 of code sample above).
Now when the user opens the file (for instance txt file) it is possible to edit and save the file (it does not even ask for a fileName, because you have given when creating the File based on the blob in the above sample).
However I don't want this behaviour. I want the file save to behave as 'save as" similar to a non existing file.
In short:
I have :
A byte[] containing content of a .txt that is located on a server
I want
To be able to open the file in the associated program (notepad for instance) without actually creating the File on the client PC. When the user would press save, it should be prompted for a fileName, just like when saving a new file for the first time.
How should I go about this?
Example of behaviour I want: behaviour as Mail attachment in email client like thunderbird for example. When clicking on txt attachment it asks me to a)open with ... , or b) save to ... . If i choose open with, and then in text editor I choose save, it prompts me for a name and location.
Undoable. Even if you, a bit after opening the temporary file, would delete the file, for instance Notepad would not see it. Others would ask to recreate at the same spot.
Would an alternative not suffice: first a "create new" - a file save dialog -, you create the file where desired, and then do a Desktop.open.
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.
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.