can the users select one or more than one mp3 files using JFileChooser?
I can only select user one file , using this method.
Just set the multi-selection to true and the selection mode to JFileChooser.FILES_AND_DIRECTORIES and it will work for a multiple files and all the files in a directory:
fileChooser.setFileSelectionMode( JFileChooser.FILES_AND_DIRECTORIES );
fileChooser.setMultiSelectionEnabled(true);
Then retrieve all the files this way:
fileChooser.getSelectedFiles();
My understanding of your requirement is:
User can choose one or multiple files
If a single file is chosen, then you work with that file
If multiple files are chosen then you would create a playlist and work with this playlist.
If this is what you want, I think the following might work for this scenario. Note that I've left the implementation to you because you know how to create a playlist or how to create a single file and feed it to your player.
/** This method returns a set of files chosen by the user.
* Returns null if selection is cancelled
**/
private File[] openFiles(){
JFileChooser fileChooser = new JFileChooser();
fileChooser.setMultiSelectionEnabled(true);
fileChooser.setFileSelectionMode( JFileChooser.FILES_AND_DIRECTORIES );
int optionChosen = fileChooser.showOpenDialog(this);
return (optionChosen == JFileChooser.CANCEL_OPTION) ? null : fileChooser.getSelectedFiles();
}
public void actionPerformed(ActionEvent e){
File[] selectedFiles = openFiles();
if(selectedFiles == null){
//handleNoFileChosen
}else if(selectedFiles.length == 1){
//handle single file selected
}else{
//handle creating playlist
}
}
Related
I have a simple JavaFX window with a TextField for users to enter a file path and a separate browse link.
JavaFX Window
I'd like to ask how to extract the full file path of the selected file from JavaFX FileChooser (so I can set the path in the TextField)?
I understand what I'm trying to achieve can be done simply with Swing JFileChooser with something like:
JFileChooser chooser = new JFileChooser();
String someString = chooser.getSelectedFile().toString();
But since my application is in JavaFX I want it to have a consistent look and not a mix with Swing.
I've looked through the documentation, there doesn't seem to be a method for this https://docs.oracle.com/javase/8/javafx/api/javafx/stage/FileChooser.html
Thanks in advance.
Here's another documentation. What you get in return from using showOpenDialog is a File object.
public File showOpenDialog(Window ownerWindow)
Shows a new file open dialog. The method doesn't return until the
displayed open dialog is dismissed. The return value specifies the
file chosen by the user or null if no selection has been made. If the
owner window for the file dialog is set, input to all windows in the
dialog's owner chain is blocked while the file dialog is being shown.
A file object has various methods like e. g. getAbsolutePath.
Use showOpenDialog or showSaveDialog (depending on whether you want to open an existing file or save a new one). Both return a File object.
In the controller class where you have the TextField you can create a method like so:
public void getTheUserFilePath() {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Upload File Path");
fileChooser.getExtensionFilters().addAll(
new FileChooser.ExtensionFilter("ALL FILES", "*.*"),
new FileChooser.ExtensionFilter("ZIP", "*.zip"),
new FileChooser.ExtensionFilter("PDF", "*.pdf"),
new FileChooser.ExtensionFilter("TEXT", "*.txt"),
new FileChooser.ExtensionFilter("IMAGE FILES", "*.jpg", "*.png", "*.gif")
);
File file = fileChooser.showOpenDialog(dialogPane.getScene().getWindow());
if (file != null) {
// pickUpPathField it's your TextField fx:id
pickUpPathField.setText(file.getPath());
} else {
System.out.println("error"); // or something else
}
}
I want to select the folder that is selected.
JFileChooser targetDir = new JFileChooser();
targetDir.setDialogTitle("Choose Target Directory.");
targetDir.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if(targetDir.showOpenDialog(null)==JFileChooser.APPROVE_OPTION)
{
System.out.println(targetDir.getCurrentDirectory());
main_mw = new MainWindow("XYZ Copier");
main_mw.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
} else {
System.exit(0);
}
} else {
}
It gives the output "/home/rahul/Downloads/mc"
but I need "/home/rahul/Downloads/mc/lib". It gives same result if i go inside lib.
Screenshots:
JFileChooser#getSelectedFile will return the selected file/directory
getCurrentDirctory returns the directory which is currently been shown in the chooser
I built GUI in java , that the user can load files from it.
now i want to pass those file to script that already exists , but the script until now let the user to choose file in the script and i want to prevent that and pass the file directly from the java GUI.
is it possible?
for example:
if(click.getSource() == chooseLog){
JFileChooser fc3 = new JFileChooser();
if(fc3.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
LOG = fc3.getSelectedFile();
fc3Bol = true;
}
}
if(click.getSource() == analayze){
if(fc3Bol == true){
// here i want to start vbscript that will get the LOG file
}
}
i created an html editor and i want to get the file name & path of opened file in the JTextPane. any suggestion?
Assuming you make use of the File chooser (the file picker), which seems quite likely for a code editor you could simply save the file path you receive as a result:
public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(FileChooserDemo.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//At this point you can use: file.getName() to get your filename
//You can also use file.getPath()
} else {
//Canceled opening
}
}
}
You could save the result of file.getName() and file.getPath() to a string that you would assign to your JTextPane later on.
For additional information on file choosers see the documentation which also explains this process in more detail.
Should you be working with File you can make use of the same functions which will provide the same information.
I made a simple application to open only XML files using JFileChooser. How can I show the open dialog again and again until I open correct XML file or press cancel button?
You could add a file filter to the file chooser that checks whether the file is an xml file.
When the user has selected a file you check that file's content and if it isn't valid you just open the filechooser again, e.g. in a loop that exits when either the file is valid or the user selected the cancel option.
Basically the loop might look like this (that's quickly written and might contain errors):
int option = CANCEL_OPTION;
boolean fileIsValid = false;
do {
option = filechooser.showOpenDialog(); //or save?
if( option == OK_OPTION ) {
fileIsValid = isValid( filechooser.getSelectedFile()); //implementation of isValid() is left for you
}
} while( option == OK_OPTION && !fileIsValid);
This loop does the following:
it opens the filechooser and gets the selected option
when the OK option is selected the selected file is checked
when the OK option was selected but the selected file is invalid, do another iteration - otherwise end the loop (if another option, e.g. CANCEL, was selected or the file is valid)
Keep opening the dialog until cancel is pressed or a valid file is chosen. You have to implement isValidFile yourself:
do {
int returnVal = chooser.showOpenDialog(parent);
} while (returnVal != JFileChooser.CANCEL_OPTION || !isValidFile(chooser.getSelectedFile()));
What about this Solution:
It open filechooser and checks if it was not a CANCEL_OPTION. If your check for the correct XML File was successful, then you break the while loop.
JFileChooser fc = new JFileChooser();
int returnVal = -1;
while (returnVal != JFileChooser.CANCEL_OPTION) {
returnVal = fc.showOpenDialog(putYourParentObjectHere);
if (returnVal == JFileChooser.APPROVE_OPTION) {
if (doYourCheckIfCorrectXMLFileWasChosenHere) {
// do the stuff you want
break;
}
}
}