How to browse folders in java? - 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).

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

Detecting Audio File Bit Rate - Processing / Java

Trying to build a little app for sorting through audio files based on some of their properties. Have managed to grab the Sample Rate and Bit Depth using Minim but can't find anything anywhere for getting the Bit Rate?
Happy to look at taking the program to Javascript if needed but just desperate to find a method for detecting bit rate of a given file.
EDIT: Attempted to try and form an equation based off file size but cannot find a method for detecting MP3 file size either.
You can use jaudiotagger
You will need to download the jar, I managed to get it from maven central
Go to Sketch -> Add File... and select the downloaded jar, it should be added in a folder named code within your sketch folder.
Assuming you have placed an mp3 file in your data folder named audio.mp3 the following code should work, printing out the bit rate in the terminal.
import org.jaudiotagger.audio.mp3.*;
import org.jaudiotagger.audio.AudioFileIO;
void setup() {
File f = new File(dataPath("audio.mp3"));
try {
MP3File mp3 = (MP3File) AudioFileIO.read(f);
MP3AudioHeader audioHeader = mp3.getMP3AudioHeader();
println("" + audioHeader.getBitRate());
}
catch(Exception e) {
e.printStackTrace();
}
}
JAudiotagger supports a variety of file formats and you can use the relevant classes and methods for each one of these.
I suggest you take a look at the javadoc. Be careful of the examples though, the one I used in order to answer your question seems to be faulty, as you can see I had to swap getAudioHeader with getMP3AudioHeader.

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

Java JFileChooser getAbsoluteFile Add File Extension

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.

Categories

Resources