JFileChooser returns not all path - java

using following method on Path button click:
public static void pathButtonAction() {
JFileChooser chooser = new JFileChooser();
if (pathToInbound == null) { //private static File pathToInbound;
chooser.setCurrentDirectory(new java.io.File("."));
} else {chooser.setCurrentDirectory(pathToInbound);
}
chooser.setDialogTitle("Choose folder with messages to send");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
pathToInbound = chooser.getCurrentDirectory();
addLogText(chooser.getCurrentDirectory().getAbsolutePath());
}
}
But here i choose folder c:\windows\temp
Here addLogText(chooser.getCurrentDirectory().getAbsolutePath()) i get to log only c:\windows. Why temp folder was ignored/truncated?

You should call chooser.getSelectedFile() instead of chooser.getCurrentDirectory(), this returns the current directory where the user has navigated in the filechooser. In your case it is C:\Windows.

Related

JFileChooser can select any folder apart from the current one?

I am using JFileChooser to allow the user to select a folder. They must be able to view the files in each folder for context. The problem is that I can't select the folder I am when the dialog pops up. (i.e. I click "open" and nothing happens). However, if I switch to another directory and then back to the first one, then I can select it.
public static String selectFolder()
{
final JFileChooser chooser = new JFileChooser() {
public void approveSelection() {
if (getSelectedFile().isFile()) {
return;
} else
super.approveSelection();
}
};
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle("Select Folder");
chooser.setFileSelectionMode( JFileChooser.FILES_AND_DIRECTORIES );
chooser.setAcceptAllFileFilterUsed(false);
chooser.showOpenDialog(null);
File x = chooser.getSelectedFile();
if( x != null )
return x.toString();
return null;
}
public static String selectFolder() {
final JFileChooser chooser = new JFileChooser() {
public void approveSelection() {
if (getSelectedFile().isFile()) {
return;
} else
super.approveSelection();
}
};
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle("Select Folder");
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.setAcceptAllFileFilterUsed(true);
chooser.setSelectedFile(new java.io.File("."));
chooser.showOpenDialog(null);
File x = chooser.getSelectedFile();
if (x != null)
return x.toString();
return null;
}
you only have to add the line:
chooser.setSelectedFile(new java.io.File("."));
for the sake of being user-friendly, set it to the same as the CurrentDirectory, so that the user sees which directory will be selected when he clicks the button
As per JFileChooser you have to choose a file or folder on the dialog then only it will allow you to click open/save.

how to get a JFileChooser to remember a previous folder?

I'm trying to get the JFileChooser to remember the location of the previous location opened, and then next time open there, but is doesn't seem to remember. I have to open it twice:
At the first run it works fine. But at the second run there's still the path locked from the first run. I have to open the JFileChooser dialog twice to get the newer path...
//Integrate ActionListener as anonymous class
this.openItem.addActionListener(new java.awt.event.ActionListener() {
//Initialise actionPerformed
#Override
public void actionPerformed(java.awt.event.ActionEvent e) {
//Generate choose file
this.chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = this.chooser.showOpenDialog(PDFcheck.this.openItem);
if (this.theOutString != null){
this.chooser.setCurrentDirectory(new File(this.theOutString)); }
if(returnVal == JFileChooser.APPROVE_OPTION) {
//theOutString = fc.getSelectedFile().getName();
this.theOutString = this.chooser.getSelectedFile().getPath();
System.out.println("You chose to open this file: " + this.theOutString);}
}
private String theOutString;
private final JFileChooser chooser = new JFileChooser();
});
thanks ;-)
Problem is that you first show the file chooser dialog, and you only set its current directory after that.
You should first set the current directory first and then show the dialog:
if (this.theOutString != null)
this.chooser.setCurrentDirectory(new File(this.theOutString));
int returnVal = this.chooser.showOpenDialog(PDFcheck.this.openItem);

JFileChooser causes the application into Busy/Not responding state

I am using JFileChooser to browse and load a file.
File chooser is loading properly and /i was able to select the file and load as well.
The issue is soon after opening the JFilechooser, the background window of the application goes in to Busy/Not Responding state. And if I Drag the
file chooser window, the background is not drawing properly. It causes the application into the crash state. Any idea why it happens.
Please find my below code
LoadFileButton.addListener(SWT.Selection, new Listener() {
#Override
public void handleEvent(Event event) {
JFileChooser chooser = new JFileChooser();
chooser.setFileFilter(filterWithoutExtension);
chooser.setAcceptAllFileFilterUsed(false);
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
File selectedFile = null;
int returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
//Do My Stuff
}
}
});
Please find my FileFilter
FileFilter filterWithoutExtension = new FileFilter() {
#Override
public boolean accept(File f) {
return !f.getName().contains(".");
}
#Override
public String getDescription() {
return "My Description";
}
};

Filter file types with JFileChooser

I am using JFileChooser to select a file and I am trying to limit the display to show only jpg or jpeg files. I have tried FileFilter and ChoosableFileFilter and it is not limiting the file selection. Here is my code:
JFileChooser chooser = new JFileChooser();
FileFilter filter = new FileNameExtensionFilter("JPEG file", new String[] {"jpg", "jpeg"});
chooser.setFileFilter(filter);
chooser.addChoosableFileFilter(filter);
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
debug.put("You chose to open this file: " + chooser.getSelectedFile().getAbsolutePath());
File selectedFile = new File(chooser.getSelectedFile().getAbsolutePath());
...
Try this:
import javax.swing.JFileChooser;
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(new FileFilter() {
public String getDescription() {
return "JPG Images (*.jpg)";
}
public boolean accept(File f) {
if (f.isDirectory()) {
return true;
} else {
String filename = f.getName().toLowerCase();
return filename.endsWith(".jpg") || filename.endsWith(".jpeg") ;
}
}
});
Do you mean "it's not limiting the selection" as in "it's allowing the option for any file type"? If so, then try JFileChooser.setAcceptAllFileFilterUsed(boolean).
chooser.setAcceptAllFileFilterUsed(false);
According to the JFileChooser documentation, it should tell it not to add the all-file-types file filter to the file filter list.
Try and use fileChooser.setFileFilter(filter) instead of fileChooser.addChoosableFileFilter(filter).
Try to use fileChooser.setFileFilter(filter) after fileChooser.addChoosableFileFilter(filter), because you need to add your filter to fileChooser and then setting it as default value.
Here is the link with good example:
http://www.java2s.com/Code/Java/Swing-JFC/CustomizingaJFileChooser.htm
Here is a sample code!
private void btnChangeFileActionPerformed(java.awt.event.ActionEvent evt) {
final JFileChooser fc = new JFileChooser();
fc.addChoosableFileFilter(new ArffFilter());
int returnVal = fc.showOpenDialog(this);
...
}
Then
class ArffFilter extends FileFilter {
#Override
public boolean accept(File file) {
if (file.isDirectory()) {
return true;
}
String fileName = file.getName();
int i = fileName.lastIndexOf('.');
if (i > 0 && i < fileName.length() - 1) {
if (fileName.substring(i + 1).toLowerCase().equals("arff")) {
return true;
}
}
return false;
}
#Override
public String getDescription() {
return ".arff (Weka format)";
}
}

Unable to figure how to store "Save As" text in a string and use it

I am new to UI design in Java. I am trying to create a GUI to download a file off the Internet and save it on your hard drive. I have got the code working except for one thing which I want to add. I have added a JFileChooser which lets the user select the destination folder. But I am unable to figure out how to change the filename to the one which user enters in the Save As bar on the JFileChooser menu.
Browse Button
browseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
chooser = new JFileChooser();
chooser.setCurrentDirectory(null);
chooser.setDialogTitle("Select folder to save");
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.setAcceptAllFileFilterUsed(true);
//chooser.showDialog(downloadButton, "Save");
if(chooser.showSaveDialog(downloadButton) == JFileChooser.APPROVE_OPTION)
{
System.out.println("The location to save is: " + chooser.getCurrentDirectory());
DESTINATION_FOLDER = chooser.getCurrentDirectory().toString();
}
}
});
Download Button
URLConnection connection = downloadUrl.openConnection();
input = new BufferedInputStream(connection.getInputStream());
output = new FileOutputStream(DESTINATION_FOLDER + "/" + filename);
Here filename should be the one which user enters. Pointers on how to get this done?
Actually you don't need to get the FileName from the Save As Bar in the JFileChooser.
Just do like this:
browseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
chooser = new JFileChooser();
chooser.setCurrentDirectory(null);
chooser.setDialogTitle("Select folder to save");
//Don't use the 'FileSelectionMode();'. Let it be Default.
chooser.setAcceptAllFileFilterUsed(true);
if(chooser.showSaveDialog(downloadButton) == JFileChooser.APPROVE_OPTION)
{
file = chooser.getSelectedFile();
//file should be declared as a File.
System.out.println("The location to save is: " + chooser.getCurrentDirectory();));
System.out.println("The FileName is: " + file.getName());
}
}
DOWNLOAD BUTTON:
URLConnection connection = downloadUrl.openConnection();
input = new BufferedInputStream(connection.getInputStream());
output = new FileOutputStream(file);
Without seeing more of your code the best I could suggest is to create a Global String in the class you are working in.
public class gui extends JFrame{
public String filePath="";
public static void main(String args[]){
//button code
browseButton.addActionListener(new ActionListener())
saveAsButton.addActionListener(new ActionListener())
URLConnection connection = downloadUrl.openConnection();
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("browseButton"){
chooser = new JFileChooser();
chooser.setCurrentDirectory(null);
chooser.setDialogTitle("Select folder to save");
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.setAcceptAllFileFilterUsed(true);
//chooser.showDialog(downloadButton, "Save");
if(chooser.showSaveDialog(downloadButton) == JFileChooser.APPROVE_OPTION)
{
System.out.println("The location to save is: "+chooser.getCurrentDirectory());
filePath = chooser.getCurrentDirectory().toString();
}
else{
//save as button selected
input = new BufferedInputStream(connection.getInputStream());
output = new FileOutputStream(filePath);
}
}
}
Add this line:
chooser.setDialogType(JFileChooser.SAVE_DIALOG);
Full code:
browseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
chooser = new JFileChooser();
chooser.setCurrentDirectory(null);
chooser.setDialogTitle("Select folder to save");
chooser.setDialogType(JFileChooser.SAVE_DIALOG);
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.setAcceptAllFileFilterUsed(true);
//chooser.showDialog(downloadButton, "Save");
if(chooser.showSaveDialog(downloadButton) == JFileChooser.APPROVE_OPTION)
{
System.out.println("The location to save is: " + chooser.getCurrentDirectory());
DESTINATION_FOLDER = chooser.getCurrentDirectory().toString();
}
}

Categories