In my Java program there is a part where the user can choose and change the working directory. The problem is that there is no action performed, when I click on 'choose' after choosing the directory path. The choosing window just stays opened.
However, when I do enter any text in the field "File Name" or choose any file in the directory and do click 'choose' the window is being closed and the directory is chosen.
My code is very simple and I really don't understand why it's not working. You can find my code here:
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(null);
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if(returnVal == JFileChooser.APPROVE_OPTION) {
stringHomeDir = chooser.getCurrentDirectory().getPath();
}
...
How can I change it so the user can easily choose a directory in the file chooser?
int returnVal = chooser.showOpenDialog(null);
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
The order of these two statements should be reversed, since the showOpenDilaog method blocks until it closes.
Change the order of statements and use setSelectedFile(File) method of JFileChooser class.
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setSelectedFile(new File(chooser.getCurrentDirectory() + "/" + "Downloads")));
int returnVal = chooser.showOpenDialog(null);
...
'MyDocuments' is a default current directory of JFileChooser and 'Downloads' is a subdirectory of 'MyDocuments'.
Related
I have created JFileChooser and I need to add search functionality when a user browse for a particular folder to locate a certain file with ease such the JFilechooser should have a text field where user can type and filter other files in a given directory. How can I achieve this in java?
Here is My sample code that implements basic file browsing.
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Excell File Only", "csv");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
if (chooser.getSelectedFile() != null) {
path.setText(chooser.getSelectedFile().getName());
}}
I have a simple JFileChooser set up in the following manner
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.setFileFilter(new FileFilter() {
...
});
int v = chooser.showOpenDialog(this);
if (v == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
System.out.println(file.getAbsolutePath());
}
As you can see, this FileChooser starts out in the current directory, which in my Netbeans project, is the root of the project folder. Here's the problem: When I select a file and it prints out the absolute path, it includes the "." in the path. For instance, the output I get is:
/Users/MyName/Folder1/Folder2/./Temp.xls
Of course, this is weird, especially since I'm displaying this to the user. Now, I could be hacky and do some fun post substring processing stuff to get rid of that "/./" portion. But...is there a non-lazy programmer way to fix this problem? Thanks in advance!
Use the system property "user.dir" as follows:
File workingDirectory = new File(System.getProperty("user.dir"));
chooser.setCurrentDirectory(workingDirectory);
I am exploring Jfilechooser.I already get the file path in the Jfilechooser.Now I want to display the information like file name, file size, location, and access rights.Is their anyway to display those information using the file path only.Anyone can help me?I want it to display in a TextArea.
this is the how I pop up a Jfilechooser.
private void browsebuttonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File f = chooser.getSelectedFile();
String filename = f.getAbsolutePath();
fieldlocation.setText(filename);
}
Take a look at the JavaDoc for File:
File.getName()
Will return the name of the file
File.length()
Will return the size of the file in bytes
File.getAbsolutePath()
Will return the absolute path of the file
File.canRead()
File.canWrite()
File.canExecute()
Will return your access rights on the file.
One thing I would note about your code is that you don't check the return value from the file chooser. If the user clicks cancel you probably want to abort processing. The way to do this is to check the return value of JFileChoose.showOpenDialog(null); like so:
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
chooser.getSelectedFile().getName());
}
Straight from the JavaDoc.
In short I would suggest that you (re)?read the documentation for the APIs you are using. It will save you much time in the long run if you understand your own code.
I want to make a list with filename from a folder and show all the files that are present in that folder with a particular extension. I want the list to be selectable so that I can select and delete the file from the list or edit it. I know how to select all files from a folder but don't know how to show it in GUI.
File folder = new File("c:/");
File[] listOfFiles = folder.listFiles();
This example shows how to enumerate the files in a directory and display them in a JToolBar and a JMenu. You can use an Action, such as RecentFile, to encapsulate behavior for use in your ListModel and ListSelectionListener.
You get all the file name from folder with extension and construct a string
array out of that.Then use a JList to populate in swing.For example something
like below
String options = { "apple.exe", "ball.exe" "cat.exe"};
JList optionList = new JList(options);
Hope this will help you.
See JFileChooser (shameless copy of the JFileChooser help page):
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG & GIF Images", "jpg", "gif");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
chooser.getSelectedFile().getName());
}
See the FilenameFilter?
setMultiSelectionEnabled (true); is another hint.
Location: java/docs/api/javax/swing/JFileChooser.html
i m trying to develop an gui application which display tree of filesystem on left side and on right side it display selected tree nodes (folder)'s content .
can any body tell me to do modification in jfilechooser to just to display folder content
thank you in advance
The JFileChooser#accept allows you to filter which files are displayed. A similar method is the JFileChooser#setFileFilter method
See: example
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
Perhaps this is what you expected:-
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
try {
// Create a File object containing the canonical path of the desired directory
File f = new File(new File(".").getCanonicalPath());
// Set the current directory
chooser.setCurrentDirectory(f);
} catch (IOException e1) {
e1.printStackTrace();
}
// Show the dialog; wait until dialog is closed
int returnVal = chooser.showOpenDialog(frame);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
File f = chooser.getSelectedFile();
textField.setText(f.getAbsolutePath());
File[] contents = f.listFiles();
for(int file=0;file<contents.length;file++)
{
System.out.println(contents[file].getName()); //here you get the contents of the selected directory
}
}