This question is answered.


Permlink Replies: 15 - Pages: 2 [ 1 2 | Next ] - Last Post: 7 Feb 21, 19:06 Last Post By: JeffTucker Threads: [ Previous | Next ]
AndreWolff

Posts: 1,289
Registered: 14-Dec-2007
What returns showOpenDialog if the user hits the Cancel button.
Posted: 5 Feb 21, 12:54
 
  Click to reply to this thread Reply
I see that I get a crash if I select the cancel option in an Open file dialog:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at se.datadosen.component.JXFileChooser.getSelectedFile(JXFileChooser.java:168)
	at FancyBox.getRelativePath(FancyBox.java:121)
	at FancyBox$1$4$1$2.actionPerformed(FancyBox.java:1395)


I use this code:
            fc.showOpenDialog(window);                    
            if (fc.getSelectedFile() != null) {
                linkField.setText(IO.relativePath(fc.getSelectedFile(), myOutputdirectory));
            }

If I look in the documentation I see

showOpenDialog
public int showOpenDialog​(java.awt.Component parent)
Specified by:
showOpenDialog in interface PathChooser
Returns:
Same as JFileChooser show... methods
Sorry but the text “Same as JFileChooser show... methods“ does not help me!
Why is there here not a link to the possible return values?

Anyway what should I change in my code to avoid the crash?
RobM

Posts: 3,808
Registered: 4-Aug-2006
Re: What returns showOpenDialog if the user hits the Cancel button.
Posted: 5 Feb 21, 13:40   in response to: AndreWolff in response to: AndreWolff
Correct
  Click to reply to this thread Reply
See the Code snippets page and search for FileChooser Filter. It shows how to check if the action was completed or not.
AndreWolff

Posts: 1,289
Registered: 14-Dec-2007
Re: What returns showOpenDialog if the user hits the Cancel button.
Posted: 5 Feb 21, 15:05   in response to: RobM in response to: RobM
 
  Click to reply to this thread Reply
RobM wrote:
See the Code snippets page and search for FileChooser Filter. It shows how to check if the action was completed or not.
Yes Rob that helps!
Nothing better as learning by example!

Thanks for your help and posting these Code Snippets!

RobM

Posts: 3,808
Registered: 4-Aug-2006
Re: What returns showOpenDialog if the user hits the Cancel button.
Posted: 5 Feb 21, 17:23   in response to: AndreWolff in response to: AndreWolff
 
  Click to reply to this thread Reply
AndreWolff wrote:
RobM wrote:
See the Code snippets page and search for FileChooser Filter. It shows how to check if the action was completed or not.
Yes Rob that helps!
Nothing better as learning by example!

Thanks for your help and posting these Code Snippets!

That might be a good page to bookmark, it has lots of interesting code blocks.
AndreWolff

Posts: 1,289
Registered: 14-Dec-2007
Re: What returns showOpenDialog if the user hits the Cancel button.
Posted: 5 Feb 21, 18:28   in response to: RobM in response to: RobM
 
  Click to reply to this thread Reply
RobM wrote:
it has lots of interesting code blocks.
Like finding the output folder which corresponds with an image folder in an album with folders?
davidekholm

Posts: 3,440
Registered: 18-Oct-2002
Re: What returns showOpenDialog if the user hits the Cancel button.
Posted: 5 Feb 21, 18:51   in response to: AndreWolff in response to: AndreWolff
 
  Click to reply to this thread Reply
AndreWolff wrote:
RobM wrote:
it has lots of interesting code blocks.
Like finding the output folder which corresponds with an image folder in an album with folders?

rootOutputDirectory = engine.getInterpretedOutputDirectory();
origFile = new File(rootOutputDirectory, ao.getPathFromRoot());
AndreWolff

Posts: 1,289
Registered: 14-Dec-2007
Re: What returns showOpenDialog if the user hits the Cancel button.
Posted: 6 Feb 21, 09:17   in response to: davidekholm in response to: davidekholm
 
  Click to reply to this thread Reply
davidekholm wrote:
rootOutputDirectory = engine.getInterpretedOutputDirectory();
origFile = new File(rootOutputDirectory, ao.getPathFromRoot());
That looks good, but if I apply it in FancyBoxCustom UI in next code:
         linkAudioSelector.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                File rootOutputDirectory = engine.getInterpretedOutputDirectory();
                File rootDir = new File(rootOutputDirectory, currentAO.getPathFromRoot());
                fancyBox.getRelativePath(audioFolder, FancyBox.audioFilter, rootDir);
            }
        });
I get a red line below 'engine.' (cannot find symbol).
Fix import does not help and if I remove the line ' File rootOutputDirectory = engine.getInterpretedOutputDirectory()' I get a line under (rootOutputDirectory, which is strange because rootOutputDirectory is a global varaible (see wiki and I use rootOutputDirectory in FancyBox.java without calling engine.getInterpretedOutputDirectory();

Edit:

I get the correct result, relative path ../../FB_Samples_AA/FreeMusic/bensound-thejazzpiano.mp3, if I define rootDir as
 File rootDir = new File( "I:\\Users\\Andre\\Documents\\Mijn websites\\jalbum.net\\Terschelling_FB\\Koegelwieck");
for this album, so I need code to construct such a rootDir file for each folder.
In fact all I need is the the path of the folder output directory

Edited by: AndreWolff on 06-Feb-2021 11:02
RobM

Posts: 3,808
Registered: 4-Aug-2006
Re: What returns showOpenDialog if the user hits the Cancel button.
Posted: 6 Feb 21, 21:54   in response to: AndreWolff in response to: AndreWolff
 
  Click to reply to this thread Reply
I don't know if this is the correct way, but in FancyBoxCustomUI.java change
protected FancyBoxCustomUI(FancyBox fancyBox, JAlbumContext context { 
to
protected FancyBoxCustomUI(FancyBox fancyBox, JAlbumContext context, AlbumBean engine) {
You will be asked/need to import se.datadosen.jalbum.AlbumBean;

And in FancyBox.java change
FancyBoxCustomUI CustomUI = new FancyBoxCustomUI(this, context);
to
FancyBoxCustomUI CustomUI = new FancyBoxCustomUI(this, context, engine);
AndreWolff

Posts: 1,289
Registered: 14-Dec-2007
Re: What returns showOpenDialog if the user hits the Cancel button.
Posted: 6 Feb 21, 22:44   in response to: RobM in response to: RobM
 
  Click to reply to this thread Reply
Well Rob, I solved the problem in a clumsy way:

I did create in FancyBox.java a new function:
    public String rootOutputDir () {
        return rootOutputDirectory.toString();
    }
and called this function in FancyBoxCustomUI:
        linkAudioSelector.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
               File folderDir = new File(fancyBox.rootOutputDir(),currentAO.getPathFromRoot().toString());             
               fancyBox.getRelativePath(audioFolder, FancyBox.audioFilter, folderDir);
            }
        });
This works!

I will try your proposal tomorrow.

AndreWolff

Posts: 1,289
Registered: 14-Dec-2007
Re: What returns showOpenDialog if the user hits the Cancel button.
Posted: 7 Feb 21, 12:14   in response to: RobM in response to: RobM
 
  Click to reply to this thread Reply
Sorry Rob, I am not going to test your solution (but I think it will work), because I found a more elegant solution:

I did remove rootOutputDirectory from the custom panel code and add this in the main module:
    void getRelativePath(JTextField linkField, FileFilter filter, String folderDir) {
        File myOutputdirectory = new File(rootOutputDirectory, folderDir);
        if (rootOutputDirectory != null && rootOutputDirectory.toString().length() > 0) {
            if (!myOutputdirectory.exists()) {
                window.ui2Engine();
                myOutputdirectory = engine.getInterpretedOutputDirectory();
            }
        } else {
            window.ui2Engine();
            myOutputdirectory = engine.getInterpretedOutputDirectory();
        }
        if (!myOutputdirectory.exists()) {
            window.messageBox("Non existing output directory, select first the output directory in the next dialogue box");
            DirectoryChooser dc = ChooserFactory.createDirectoryChooser(window);
            dc.showOpenDialog(window);
            if (dc.getSelectedFile() != null) {
                myOutputdirectory = dc.getSelectedFile();
            } else {	// user cancelled the directory chooser
                return;
            }
        }
        if (filter.getDescription().startsWith("directory")) {
            DirectoryChooser dc = ChooserFactory.createDirectoryChooser(window);
            dc.setCurrentDirectory(myOutputdirectory);
            dc.showOpenDialog(window);
            if (dc.getSelectedFile() != null) {
                linkField.setText(IO.relativePath(dc.getSelectedFile(), myOutputdirectory));
            }
        } else {
            FileChooser fc = ChooserFactory.createFilesChooser(window);
            fc.setCurrentDirectory(myOutputdirectory);
            if (!filter.getDescription().startsWith("all files")) {
                fc.setFileFilter(filter);
            }
            int fcReturn = fc.showOpenDialog(window);
            if (fcReturn == JFileChooser.APPROVE_OPTION) {
                linkField.setText(IO.relativePath(fc.getSelectedFile(), myOutputdirectory));
            }
        }
    }
In the custom panel code I call this like:
       linkTracklogSelector.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                fancyBox.getRelativePath(linkTracklogFolder, FancyBox.gpsFilter,  currentAO.getPathFromRoot());           
            }
        });
and in the root it is called as
        linkTracklogSelector.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                getRelativePath(linkTracklog, gpsFilter, "");
            }
       });


This works good. If you wish, you can add it to your Code snippets page.

I am glad that I had not to code JAVA in my professional time!
Without the help of David, I could never come so far!
I don't understand why a global variable rootOutputDirectory can't be used in custom panel code.
It is for me also weird that a variable from FancyBox.java used in FancyBoxCustomUI.java gets the prefix FancyBox like FancyBox.gpsFilter and a function gets the prefix fancyBox like fancyBox.getRelativePath(linkTracklogFolder
JeffTucker

Posts: 8,037
Registered: 31-Jan-2006
Re: What returns showOpenDialog if the user hits the Cancel button.
Posted: 7 Feb 21, 14:00   in response to: AndreWolff in response to: AndreWolff
 
  Click to reply to this thread Reply
AndreWolff wrote:
I don't understand why a global variable rootOutputDirectory can't be used in custom panel code.

Because when a user is entering settings for the skin, or information for one object (in a custom panel), the location of the output directory isn't relevant. After all, the user might make all his entries, then decide to change the location of the output directory, then hit Make Album. ETA: Or he might decide to move one of his folders so that it's now a subfolder of another folder, then hits Make Album. If you've been calculating relative paths, they are now wrong.

Things like that should be taken care of in the template files: init.bsh, prediir.bsh, postdir.bsh, and finally.bsh. Then, you know where the output is.
AndreWolff

Posts: 1,289
Registered: 14-Dec-2007
Re: What returns showOpenDialog if the user hits the Cancel button.
Posted: 7 Feb 21, 17:40   in response to: JeffTucker in response to: JeffTucker
 
  Click to reply to this thread Reply
JeffTucker wrote:
Because when a user is entering settings for the skin, or information for one object (in a custom panel), the location of the output directory isn't relevant.
The same holds for the main module FancyBox.java, so why is it allowed to use rootOutputDirectory in FancyBox.java but not in FancyBoxCustomUI.java ?
JeffTucker

Posts: 8,037
Registered: 31-Jan-2006
Re: What returns showOpenDialog if the user hits the Cancel button.
Posted: 7 Feb 21, 17:47   in response to: AndreWolff in response to: AndreWolff
 
  Click to reply to this thread Reply
Just because you can do something doesn't mean that you should do it.

When entering skin options of any kind, relying on the location of any of the output is a mistake. The user might move the output, he might move folders around, he might delete the entire local album (to make sure that he's cleared out the debris from earlier experiments), etc. In short, when you're accepting users' inputs to the project, the output doesn't exist yet, so you can't safely make any assumptions about it.
RobM

Posts: 3,808
Registered: 4-Aug-2006
Re: What returns showOpenDialog if the user hits the Cancel button.
Posted: 7 Feb 21, 18:17   in response to: AndreWolff in response to: AndreWolff
 
  Click to reply to this thread Reply
AndreWolff wrote:
JeffTucker wrote:
Because when a user is entering settings for the skin, or information for one object (in a custom panel), the location of the output directory isn't relevant.
The same holds for the main module FancyBox.java, so why is it allowed to use rootOutputDirectory in FancyBox.java but not in FancyBoxCustomUI.java ?
FancyBox.Java includes ‘extends compiledBase’, rootOutputDirecty and engine are part of that (compiledBase) class. FancyBoxCustomUI.java extends JCustomPanel, which is a swing component and thus a different class. Java is object based, not a procedural language such as Basic, C, Pascal etc. Try searching the web for ‘ Differences between Procedural and Object Oriented Programming’
davidekholm

Posts: 3,440
Registered: 18-Oct-2002
Re: What returns showOpenDialog if the user hits the Cancel button.
Posted: 7 Feb 21, 18:47   in response to: RobM in response to: RobM
 
  Click to reply to this thread Reply
Very relevant comments from jGromit.
Another note: "engine" is available via "context". Just call context.getEngine()
Legend
Forum admins
Helpful Answer
Correct Answer

Point your RSS reader here for a feed of the latest messages in all forums