I am using JavaFX to write a script, part of which involves the user selecting an existing project, or creating a new one. In the latter case I have a popup window that prompts the user for a name and a place to save the project. What I hope to do is create a folder in the place that the user specifies with the same name as the project, from here I will save different items to it and load them later. However when I use filechooser to have the user pick the location for the folder it requires them to select an item before the chooser actually picks anything. Then the folder that it makes has the name of this item in the beginning. (For example if I picked the Chrome shortcut on my desktop the folder name would be chrome.exeProject1 or something like that)
I know part of the problem is because I reused part of my filechooser code for a different function where I required the user to have an item selected before they could submit it, anyway...
Is there any way for the user to select a folder, or just submit the filechooser without selecting any items and create the new folder here? (Inside the selected folder or in the current folder in the latter case).
relevant code below, any help is appreciated. Thanks!
#FXML
private void submitName(ActionEvent event) {
name = nameOfProject.getText();
if (name != null && location != null)
{
new File(location + name).mkdir();
}
}
#FXML
private void createProjectDirectory(ActionEvent event) {
FileChooser projectDirectory = new FileChooser();
projectDirectory.setTitle("Create Project Directory:");
File refLocation = projectDirectory.showOpenDialog(null);
if (refLocation != null) {
locationOfProject.clear();
location = refLocation.getPath();
locationOfProject.appendText(location);
}
}
You have to use DirectoryChooser instead of FileChooser to let the user select a directory.
You can read the documentation on how to use these classes here (it's from JavaFX 2, but I guess the interface is mostly the same).
Related
Using LeanFT's Java SDK, is it possible to perform click operation on "Save" button from Chrome's "Save As" dialog?
Have you tried to use the Object Identification Tool to identify the Save button?
In any case, I understand you are now working with Web technology, then you click on a Save button, which then prompts you to save the file in a location.
You have two options:
Set Chrome to no longer ask where to save files;
Describe the Save button that you want to Click using the STD technology.
Example of such description:
Desktop.describe(Window.class, new WindowDescription.Builder()
.ownedWindow(false)
.childWindow(false)
.windowClassRegExp("Chrome_WidgetWin_1")
.windowTitleRegExp(" Google Chrome").build())
.describe(Dialog.class, new DialogDescription.Builder()
.ownedWindow(true)
.childWindow(false)
.text("Save As")
.nativeClass("#32770").build())
.describe(Button.class, new ButtonDescription.Builder()
.text("&Save")
.nativeClass("Button").build());
Then you can call the .click method on this object (or cancel, or so on).
String home = System.getenv("USERPROFILE") + "\\Downloads\\";
String fileName = "xxxxxxxxx.xls";
File file = new File(home + fileName);
if (file.exists()) {
file.delete();
System.out.println("Deleted existing CurrentSortRules.xls file if it is already present");
}
Window popup = Desktop.describe(Window.class, new WindowDescription.Builder().ownedWindow(false).childWindow(false)
.windowClassRegExp("Chrome_WidgetWin_1").windowTitleRegExp(" Google Chrome").build())
.describe(Dialog.class, new DialogDescription.Builder().ownedWindow(true).childWindow(false)
.text("Save As").nativeClass("#32770").build());
I'm making a simple Download manager in java.
When running the code in mac it works fine, but i went to test it in windows 7, and i got this problem:
If I click in save, it will leave the path to C:\Users\Gui
If I click to cancel, it will change it to C:\Users\Gui\Documents
Code that i'm using:
JFileChooser f = new JFileChooser();
f.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
f.showSaveDialog(null);
tfPath.setText(f.getCurrentDirectory().toString());
Anyone know a solution to this problem?
default directory of JFileChooser is user/documents for example
for me its
C:\Users\Adeel Ahmad\Documents
for you it will be your C:\Users\your_login_account_name\Documents
use setcurrent directory to set the default directory when jfilechooser is opened
f.setCurrentDirectory("C:\somwhere");
Your code calls f.getCurrentDirectory() , which will give you the parent directory of whichever directory you're in when selecting a directory in JFileChooser.DIRECTORIES_ONLY mode.
you might wanna use f.getSelectedFile() this is the reason you are getting C:\Users\Gui\Documents because when you save it,it gets its parent directory
and you can also know user selected save or cancel, JFileChooser.showSaveDialog returns an int which you can compare with constants of JFileChooser
you can take any action on the base of if user selected save or cancel by using
JFileChooser.CANCEL_OPTION or JFileChooser.APPROVE_OPTION(meaning save option)
JFileChooser f = new JFileChooser();
f.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
f.setCurrentDirectory(new File("D:\\"));
int result=f.showSaveDialog(null);
if(result==JFileChooser.APPROVE_OPTION)
tfPath.setText(f.getCurrentDirectory().toString());
In Swing, the JFileChooser pointed to the user's default directory which is typically the "My Documents" folder in Windows. The JavaFX FileChooser does not have the same behavior by default. There is a setInitialDirectory method which should be fine, however there are a number of places in the application that we open FileChoosers. Unfortunately the FileChooser class is final, so I cannot simply extend the class and just call the setInitialDirectory once. Is there anything else I could do besides going through the entire application and adding the setInitialDirectory calls?
There's the obvious solution, to just create a static utility method somewhere:
public class MyUtilities {
public static FileChooser createFileChooser() {
FileChooser chooser = new FileChooser();
chooser.setInitialDirectory(new File(System.getProperty("user.home"));
return chooser ;
}
}
Then you can just do
FileChooser chooser = MyUtilities.createFileChooser();
whenever you need one.
I actually prefer, from a user experience perspective, to use a single FileChooser instance for the whole application (or at least for each functional portion of a large application). That way it maintains the last directory the user visited, which is more convenient imho.
I want to know which file (or even project is enough) is opened in eclipse editor? I know we can do this once we get IEditorPart from doSetInput method,
IFile file = ((IFileEditorInput) iEditorPart).getFile();
But I want the name of file without using IEditorPart, how can I do the same?
Checking which is the selected file in project explorer is not of much help because, user can select multiple files at once and open all simultaneously and I did not way to distinguish which file opened at what time.
Adding more info:
I have an editor specified for a particular type of file, now every time it opens, during intializing editor I have some operation to do based on project properties.
While initializing editor, I need the file handle (of the one which user opened/double clicked) or the corresponding project handle.
I have my editor something this way:
public class MyEditor extends TextEditor{
#Override
protected void initializeEditor() {
setSourceViewerConfiguration(new MySourceViewerConfiguration(
CDTUITools.getColorManager(), store,
"MyPartitions", this));
}
//other required methods
#Override
protected void doSetInput(IEditorInput input) throws CoreException {
if(input instanceof IFileEditorInput)
{
IFile file = ((IFileEditorInput) input).getFile();
}
}
}
as I have done in the doSetInput() method , I want the file handle(even project handle is sufficient). But the problem is in initializeEditor() function there is no reference to editorInput, hence I am unable to get the file handle.
In the source viewer configuration file, I set the code scanners and this needs some project specific information that will set the corresponding rules.
You never have the guarantee (even with IFileEditorInput) to know which files are "open" with editors.
There is even no definition of an "opened file" there are for example editors that show the contents of multiple files (like the Plug-In Manifest Editor from PDE). Other editors show only the contents of a URI (which maybe local)
Could you explain which problem you are trying to solve?
I ran into an issue with JFileChooser and wanted to see if there is a workaround.
If the JFileChooser is created and the setFileSelectionMode is FILES_AND_DIRECTORIES, when a user clicks a shortcut button on the left (in XP) such as Desktop or My Documents or drop down to Desktop, the field is not placed in the File Name JTextPane. And when clicking the "Select/Accept" button, nothing happens (because isDirectorySelected() returns false for some reason).
Overriding the approveSeletion does not work because the Event Handler function in BasicFileChooser does not call it.
How would I make it so the Desktop can be selected without having to navigate to it manually, but by clicking the shortcut on the left?
Thanks
In Windows, the desktop is not backed by any file in the file system - it's a shell namespace. So there really isn't anything that JFileChooser could return to you. Yes, I know that there is a folder that contains the desktop for the user - but remember that the desktop actually displays as a composite of the user's desktop and the All Users desktop folder - plus other things that are added by the shell but not part of any folder (like the trash bin). So returning a File object that represents the 'desktop' is pretty much a non-starter.
Long and short: Ask yourself why you need to do this - chances are that you are going to wind up deep into native code territory, dealing with namespace PIDLs and all sorts of nastiness that you may not want to get into (for the life of me, I cannot understand why M$ had to make this stuff so amazingly difficult to use)...
Here's an intro to Windows shell namespaces so you'll have a feel for what's involved:
http://msdn.microsoft.com/en-us/library/cc144090%28v=vs.85%29.aspx
Found the following code in the BasicFileChooserUI:
if (fc.getFileSelectionMode() == JFileChooser.FILES_AND_DIRECTORIES
&& fc.getFileSystemView().isFileSystem(dir)) {
setFileName(dir.getAbsolutePath());
}
So it looks like "special folders" are purposely ignored. The code is in a private method so it would be hard to create you own UI.
As a hack you might be able to add a PropertyChangeListener to the file chooser:
public void propertyChange(final PropertyChangeEvent e)
{
String prop = e.getPropertyName();
if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(prop))
{
JFileChooser fileChooser = (JFileChooser)e.getSource();
File currentDirectory = (File)e.getNewValue();
String directory = currentDirectory.toString();
if (directory.endsWith("Desktop")
|| directory.endsWith("My Documents"))
{
File selectedFile = fileChooser.getSelectedFile();
if (selectedFile == null || ! selectedFile.equals(currentDirectory))
{
fileChooser.removePropertyChangeListener( this );
fileChooser.setSelectedFile( currentDirectory );
fileChooser.addPropertyChangeListener( this );
}
}
}
}