Adding a Search field to a Jfilechooser - java

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());
}}

Related

JFileChooser can't click 'choose'

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'.

JFileChooser - open in current directory

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);

Java: How to read "Files of type" in JFileChooser

I want to create a new file while file type is choosing by the user on JFileChooser. How this is possible. I use this code for JFileChooser:
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("Save data in your file format");
FileFilter type1 = new ExtensionFilter("Access 2007 Database(*.accdb)", ".accdb");
FileFilter type2 = new ExtensionFilter("Access 2002-2003 Database(*.mdb)", ".mdb");
chooser.addChoosableFileFilter(type1);
chooser.addChoosableFileFilter(type2);
int actionDialog = chooser.showSaveDialog(frame);
Now I an able to get the "File Name" when the user select any file by this code:
chooser.getSelectedFile().getName();
But I dont know how to get "Files of type" when user select any file.
For the convenience I also attach photo of JFileChooser:
You can get that information using JFileChooser .getFileFilter()
From the Javadocs:
Returns the currently selected file filter

How to make List with all files in folder using swing?

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

Modification to JFileChooser to just display folder content

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

Categories

Resources