Question about file selection boxes

Hey all,

I am stuck trying to get a file selection box to open in order to allow my app to save to file.
I’ve tried the following example from the documentation:

bool RootManager::SaveProjectToFile()
{
std::unique_ptr fc;
fc = std::make_unique(“Load Project…”,
File::getSpecialLocation(File::userHomeDirectory),
“*.hbp”, true, false, mainComponent);

auto folderChooserFlags = FileBrowserComponent::openMode;

fc->launchAsync(folderChooserFlags, [this](const FileChooser& chooser)
	{
		File hbpFile(chooser.getResult());
		XmlElement* xml = rootTreeNode->GetRecursiveXmlElement();

		if (!xml->writeTo(hbpFile)) {
			std::cout << "Failed to save Project .hbp File!\n";
			return false;
		}
		delete xml;
	});
return true;

}

When running it under debug mode with some breakpoints,
it runs, but it skips the block of code after “fc->launchAsync(folderChooserFlags, [this](const FileChooser& chooser)”,
so no filechooser is shown, and nothing is saved…
The launchAsync does’nt seem to do anything and does’nt run the code in it’s block/scope.

I tried using the following, by using the “browseForFileToSave()” method instead,
this is on the manual pages for the FileChooser class,
but they don’t seem to be implemented. Maybe they were deprecated and removed ?
You can only do the launchAsync now, no loading or saving with the “browse…” functions…

https://docs.juce.com/master/classFileChooser.html

So the code below won’t compile as there is no “browseForFileToSave()” method in the FileChooser class:

bool RootManager::SaveProjectToFile()
{
std::unique_ptr fc;
fc = std::make_unique(“Load Project…”,
File::getSpecialLocation(File::userHomeDirectory),
“*.hbp”, true, false, mainComponent);

auto folderChooserFlags = FileBrowserComponent::openMode;

fc->browseForFileToSave(true);

File hbpFile(fc.getResult());
XmlElement* xml = rootTreeNode->GetRecursiveXmlElement();

if (!xml->writeTo(hbpFile)) {
	std::cout << "Failed to save Project .hbp File!\n";
	return false;
}

delete xml;
return true;

}

Does anyone know how to get a filechooser dialog box to open and allow for loading/saving, or
can see what I’m doing wrong ?

I also would like to be able to use the OS’es native selection box, not a JUCE one…

Should be a trivial task not ?

Thanks a bunch!,
Terrence

The unique_ptr<FileChooser> is destroyed at the end of the scope (}), which closes the chooser dialog.

You need to ensure that the FileChooser instance lives for at least as long as the dialog should display on-screen. This normally means making the FileChooser a data member of a Component, rather than creating it at function scope. You can check the DialogsDemo to see how this might look in practice.

Thanks, that did it! :slight_smile:

Cheers,
Terrence