0

I have a Java GUI application in which the view should provide a function which asks the user to choose a path for example. The function should be blocking until the user selected the path (or also if the user cancelled).

As the function is not called in the context of the EDT Thread I use invokeAndWait. It looks something like this, where path is a private member of the view:

private String path;

public String getPath(String defaultPath)
{
    try{
        SwingUtilities.invokeAndWait( () -> {
             // Ask here the user for the path
             path = new PathSelector(defaultPath).getPath();
        }
    } catch (InvocationTargetException e) {
        return "";
    } catch (InterruptedException e) {
        return "";
    }
    return path;
}

My problem was how to pass the path, which is selected in the EDT context, to the function which was initially called and return it there. The following line is already blocking:

path = new PathSelector(defaultPath).getPath();

For the moment I solved it with a private member path, but actually I don't like this solution, cause path is more a temporary variable and has actually nothing to do with the class itself. Searching for another solution for this, I came across the SwingWorker but I couldn't figure out how I could solve with this my 'problem'.

An other idea is maybe to create an object which has a string as member with getter and setter to set this string and pass a reference of this object which could set the string member in the EDT and get it back in the getPath function to return it.

Has anyone a smoother solution?

Semaphor
  • 900
  • 2
  • 12
  • 33
  • 3
    I think you simply want a modal dialog. https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html#overview – JB Nizet Oct 25 '15 at 14:17
  • 1
    My thoughts exactly as @JBNizet. A JOptionPane for example, would work great for this. Understand that JOptionPanes can hold complex GUI's including JPanels chock full of components, if necessary, or if you don't like to use them in this situation, then a modal JDialog would be perfect. – Hovercraft Full Of Eels Oct 25 '15 at 14:17
  • 2
    `// Ask here the user for the path` If you mean a **file** path then use a `JFileChooser` (which is shown in a modal dialog as suggested by @JBNizet). – Andrew Thompson Oct 25 '15 at 14:49
  • Sorry, I was maybe too unclear what my problem is. The problem is not how to realize the gui itself, the function which gets the path is already blocking. My problem is how to pass the String variable back to the getPath function to return it. I used a member variable 'path', but as the variable is more a temporary variable, I don't like it to make it to a member variable. – Semaphor Oct 25 '15 at 16:12

1 Answers1

0

As nobody comes up with another solution, the best one that I could find out by myself is this: I create a simple object which contains the string to return. So I have a reference in both task contexts which I can use. I somebody has some comments to improve this solution, I'm open for it.

This is the class which holds the string.

public class StringCover {
  private String string = "";

  public String getString() {
    return string;
  }

  public void setString(String string) {
    this.string = string;
  }

}

And this is the code from above with this solution.

public String getPath(String defaultPath)
{
    StringCover stringCover = new StringCover();
    try{
        SwingUtilities.invokeAndWait( () -> {
             // Ask here the user for the path
             stringCover.setString(new PathSelector(defaultPath).getPath());
        }
    } catch (InvocationTargetException e) {
        stringCover.setString("");
    } catch (InterruptedException e) {
        stringCover.setString("");
    }
    return stringCover.getString();
}
Semaphor
  • 900
  • 2
  • 12
  • 33