How to clear the input of file chooser component on command line in install4j?

Viewed 49

The file chooser component always preserve the previous results on command line, just like

example file:
[/bin/example/file.txt]

If I enter a "space" or "Enter" key, the previous value "/bin/exampel/file.txt" will be chose. If I input another path, the value of "example file" will be changed, but how to empty the value of "example file" without changing my code? It's an easy thing in Windows, but I couldn't find anything that works in Linux.

If it's necessary to change my code, all I can think of is when the user input a specific string such as "[]", I call

context.setVariable("file chooser variable", null);

to clear the input manually, which doesn't seem very reasonable.

Is there any better method to clear the input?

1 Answers

In console mode, the empty string or a blank string signifies the acceptance of the default value, there is no way to interpret it as missing input.

You could use a "Console handler" form component to ask the questions yourself with something like this:

console.println("Current directory: " + context.getVariable("fileVariableName"));
if (console.askYesNo("Enter a different directory?")) {
    String newDirectory = console.askString("New directory", "");
    if (newDirectory.trim().isEmpty()) {
        context.setVariable("fileVariableName", null);
    } else {
        context.setVariable("fileVariableName", new File(newDirectory));
    }
}

return true;

Then set the "Visibility script" property of the file chooser component to !context.isConsole() so it is not displayed in console mode.

Related