getPassword from UI

Tags: #<Tag:0x00007fb882cfd798> #<Tag:0x00007fb882cfd4a0>


Hi All,

I would need a simple dialog box where a user can enter a hidden password.

In terms of java code I’d ideally like to use it as simple as this:

String password = getPasswordFromUI();

Any help is appreciated!


Hi @Christian_Tischer,

you can use a simple script declaring a single parameter that’s both input and output, like this:

#@BOTH String (label = "Please enter password", style = password) pwd

and then run it from your SciJava Command as follows:

private ScriptService scriptService;

String script = "#@BOTH String (label = \"Please enter password\", style = password) pwd";
Module module ="GetPassword.groovy", script, true).get();

String password = module.getOutput("pwd");

(You might have to have your IDE add the necessary try/catch clauses. The above code was just quickly adapted from a Groovy script as I tested this in the script editor.)

Note to other readers of this discussion: the safety aspect of this solution was discussed in this other forum topic.


Hi Jan,

I am trying to convert to java code…

What is the point of the "GetPassword.groovy"? I don’t have to save the script on disk, do I?


My attempt so far:

public class GetPasswordCommand implements Command
    @Parameter( label = "Password", style = TextWidget.PASSWORD_STYLE, persist = false, type = ItemIO.BOTH )
    private String password;

    public void run()


And then I am trying to call it somehow like this:

public class GetPasswordFromUI
    private ScriptService scriptService;

    private ModuleService moduleService;

    public String getPassword()
        // DON'T KNOW BELOW...
        Module module = ?? GetPasswordCommand.class, ??, true ).get();

        String password = (String) module.getOutput( "password" );

        return password;


But the syntax for the ) is not correct and I don’t understand how to do this from the javadoc…could you help?


You can name it dummy.groovy as well. It’s just needed by the ScriptService (currently) to determine the language of the executed script. (This will likely be improved in SciJava 3, if I understood @ctrueden correctly.)

There’s really no reason to have this as a Java command. A single line String as in my post above will do.

Please stick to my example: you don’t need ModuleService, just ScriptService.

Anyways, in case you stick to your setup, what’s the error message?


I don’t know how to call; for example, I guess I somehow have to convert the GetPasswordCommand.class to a ModuleInfo object?


This works for me (i.e. getting the context like shown below):

    public String run() throws ExecutionException, InterruptedException

        final ImageJ ij = new ImageJ();

        Context ctx = (Context) IJ.runPlugIn("org.scijava.Context", "");
        ScriptService scriptService = ctx.service( ScriptService.class );

        String script = "#@BOTH String (label = \"Please enter password\", style = password) pwd";

        Module module = "GetPassword.groovy", script, true ).get();

        String password = ( String ) module.getOutput( "pwd" );

        return password;

However, the funny thing it that the command prints the password in clear text in a big window :slight_smile:

Any idea why that happens and how to suppress that?


Declaring the parameter as #@both makes it an input and an output. And each string output is displayed in a text window. There is currently no way to suppress the display of compatible outputs.

Presumably you want this password for use in another script, no? Would it suffice to make the password a normal input (#@ String (style = password) password) in that same script?


Thanks for the explanation!

Yes, I would like to use the password in other parts of my code. My use case is that I am developing java code for spawning cluster jobs from within ImageJ. For this, the users need to enter their passwords from time to time. Thus, I need a reusable piece of code that retrieves the password in a safe way.


Note that @Parameter annotations will only be processed if the parent class is annotated as a SciJava Plugin. Also, if that is the case, you can just put your password String parameter right there as well and there’s no need to go the detour via calling a service to run another module.

If for any reason (and here I still don’t know enough about your intended use case) you need to spin up your own context with:

… then you should also be able to get the required services via that context instance, e.g.:

ScriptService scriptService = ij.script();

(Note the analogy to how you get the UIService by ij.ui() in your above code.)

Setting the boolean parameter of, String, boolean, Object...) to true means that all inputs are being pre-processed by available PreprocessorPlugins (which is what you want, to show the password dialog), but also that all outputs are being post-processed by PostprocessorPlugins (which causes their display, in case a UI is available).

As Curtis stated, there is currently not much flexibility to configure how pre- and post-processing is done, see this issue discussion on Github:

You can however mark the output as resolved, which requires that you first create a module:

Module scriptModule = moduleService.createModule(scriptInfo);

… and set its output to resolved before you run it:


You can see the batch-processor code for some example, e.g. in ModuleBatchProcessor.
I haven’t tried this with parameters annotated with ItemIO.BOTH, but I think it should work the same.

To avoid more lengthy discussions, I think it would be really helpful if you put your full code on github, so we can get the big picture of what you’re trying to achieve.


There is actually complete flexibility! :wink: The methods will accept whatever ModulePreprocessor and ModulePostprocessor instances you want! It’s just not very convenient, because you have to build the lists manually then. We don’t have an “excludes” mechanism, nor am I certain we want one.

I agree 100%. The other things @imagejan mentions here are complex and should absolutely be unnecessary for a simple use case like this.

@Christian_Tischer Why can’t you simply put a password parameter at the top of each script that needs one? Why do you need this extra getPassword thing?


That is in fact also a solution. I just needed it so often that I thought it’d be nice to have it as a simple one-liner that I can call from wherever I need it.