Java JFileChooser getAbsoluteFile Add File Extension - java

i have this issue working but i would like to know if there is a better way of adding the file extension?
what i am doing right now is:
String filePath = chooser.getSelectedFile().getAbsoluteFile() + ".html";
im adding the extension hard coded. and then saving to it.
just wondering if there is a more robust/logical manner this can be implemented?
thank you for your time.
EDIT: i ask this as i would like my app to be portable across platforms. so adding .html manually i may make this a windows only solution.
EDIT: i think ive surfed enough to know that .html hard coded is safe as i havent found any documentation that says dont take this approach (not completely sure).
ISSUE: also if i want to save the file in another format, text, for example how do i detect that the user selected which format?
FileNameExtensionFilter can add filters to the dialog but how do i get the return value for file type selected?
EDIT: i have studied this but still unclear how to retrive user selected file type.
EDIT: this is a rephrase of my issue:
alt text http://img98.imageshack.us/img98/4904/savef.jpg my question is how can i retrieve/find out which one of the two filters the user has selected as the save format. HTML or JPEG? how do i retrieve this info from JFileChooser? thank you.
EDIT: found something out: it has something to do with JFileChooser.getFileFilter()
your help still welcome.
EDIT: getFileFilter() and FileNameExtensionFilter comparasion solved this issue.

Here is the code snippet that solves the issue:
JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(false);
chooser.setAcceptAllFileFilterUsed(false);
FileNameExtensionFilter filter = new FileNameExtensionFilter("HTML Documents", "htm", "html");
chooser.setFileFilter(filter);
int option = chooser.showSaveDialog(ChatGUI.this);
if (option == JFileChooser.APPROVE_OPTION) {
// Set up document to be parsed as HTML
StyledDocument doc = (StyledDocument)textPaneHistory.getDocument();
HTMLEditorKit kit = new HTMLEditorKit();
BufferedOutputStream out;
try {
System.out.println(chooser.getFileFilter());
if (chooser.getFileFilter() == filter)
System.out.println("ha ha");
}
}

You're probably looking for this:
The trick consists in casting the returned FileFilter to FileNameExtensionFilter and then apply getExtensions().
JFileChooser fileChooser = new JFileChooser("");
// Prevent user to use the default All Files option
fileChooser.setAcceptAllFileFilterUsed(false);
[...]
// Get the FileFilter
FileFilter ff = fileChooser.getFileFilter();
// Cast the FileFilter to FileNameExtensionFilter
FileNameExtensionFilter extFilter = (FileNameExtensionFilter)ff;
// Get the Extension
String ext = extFilter.getExtensions()[0];
Or, for making it compact:
ext = ((FileNameExtensionFilter)fileChooser.getFileFilter()).getExtensions()[0];

I don't understand what it is you're trying to do. Are you trying to save the selected file in some other format than it already is of? The path of the selected file will contain the file extension, so you don't need to add it manually. The following, for example, will print "/Users/banang/Documents/anything.html" to the screen if the file anything.html is selected.
JFileChooser chooser = new JFileChooser();
chooser.showSaveDialog(null);
System.err.println(chooser.getSelectedFile().getCanonicalPath());
Please try to clarify your question a bit.

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!

JFileChooser file type filtering not working

I'm trying to filter file types in my JFileChooser like this;
JFileChooser fc = new JFileChooser();
fc.setDialogTitle(langManager.getText("programwidget.urlbutton.fd.text"));
fc.setFileFilter(new FileNameExtensionFilter(null, "ico", "png", "gif"));
But when I launch it, I can still choose another file types, so it's not working as I expected.
Can someone please tell me what I'm missing here?

Javafx filechooser name filter

i'd like to know if there's a way of filtering the names of files to make them selectable in the dialog to select files, for instance all files that starts for "A" and are in txt format, i searched a bit and i found only tips topics about the extension with the Extension filter, that's fine but i'd like to select just some file in a format.
In JavaFX you can filter for particular file types by adding ExtensionFilters to the list of filters returned by getExtensionFilters, like so:
FileChooser fileChooser = new FileChooser();
fileChooser.getExtensionFilters().add(new ExtensionFilter("Text Files", "*.txt"));
The JavaFX file chooser does not support filtering by file name, only by extension. This is because most platforms don't support this functionality natively in their file choosers.
Of course , you can get some idea from this example; in Java Swing( I'm not sure how in JavaFX) you can filter files by name or extension like :
FileChooser fileChooser = new FileChooser();
FileFilter filter = new FileNameExtensionFilter("MP3 File","mp3");
fileChooser.setFileFilter(filter)`
FileChooser chooser = new FileChooser();
chooser.getExtensionFilters().addAll(new ExtensionFilter("Excel Files", "*.xls"));
you can use add or addAll depending upon how many filters you want to add.

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 "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

Categories

Resources