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
}
}
Related
In my Project, I want to open the windows file explorer with java, in which you can select a file or a folder and click the "OK" button. Now I want to have the path of the selected file in my Javacode.
Basically like the window which pops up in every standard texteditor after you hit the "OPEN" button to choose the file to open in the editor.
I know how to open the windows file explorer with Runtime.getRuntime().exec("explorer.exe") but I canĀ“t figure out a way to return the file path.
The best option is to use JFileChooser class in javax.swing.JFileChooser. It's a Java swing object so it won't invoke the native OS's file browser but it does a good job at selecting a file/saving to a location. Take a look at this link on basic implementation of it.
I used this method from the link in my code:
public static Path getInputPath(String s) {
/*Send a path (a String path) to open in a specific directory
or if null default directory */
JFileChooser jd = s == null ? new JFileChooser() : new JFileChooser(s);
jd.setDialogTitle("Choose input file");
int returnVal= jd.showOpenDialog(null);
/* If user didn't select a file and click ok, return null Path object*/
if (returnVal != JFileChooser.APPROVE_OPTION) return null;
return jd.getSelectedFile().toPath();
}
Note that this method returns a Path object, not a String path. This can be changed as needed.
This is how I'd use a JFileChooser for your problem:
String filePath; // File path plus name and file extension
String directory; // File directory
if (returnVal == JFileChooser.APPROVE_OPTION) {
directory = fc.getSelectedFile().getName();
} else {
directory = "Error in selection";
}
filePath = directory + "\\" + folderName;
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'm using the JFileChooser to allow a user to choose a .txt file that will later be processed by my program, however when the user chooses the file, it is actually opened by my computers default app (in my case TeXworks) as well as used by my program. Any idea how I can stop this?
File fileToOpen = fileChooser.getSelectedFile();
JFileChooser's getSelectedFile() method, returns a File object.
Use the getAbsolutePath() to get the absolute name to the file.
Modified example from the JavaDoc:
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int returnVal = chooser.showOpenDialog(parent);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
System.out.println("You chose to open this directory: " + chooser.getSelectedFile().getAbsolutePath());
}
So in your case you just need to append .getAbsolutePath() to the end of your statement, like this:
File fileToOpen = fileChooser.getSelectedFile().getAbsolutePath();
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
}
}
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;
}
}
}