Fork us on GitHub

Not A Dialog - Again

When we introduced the idea of the EDT from Swing (and pretty...

Not A Dialog - Again

Picture
When we introduced the idea of the EDT from Swing (and pretty much any modern UI toolkit) into what eventually became Codename One, one of the chief benefits was modal dialogs. Its the ability to block the executing thread in order to ask the user a question, that's a very powerful tool for a developer. As a result of that we defined modal dialogs the way Swing/AWT defined them: stopping the execution of the code.

However, there is another definition... Dialogs that block the rest of the UI...
In that regard all dialogs in Codename One are modal; they block the parent form since they are really just forms that show the "parent" in their background as a simple image. A while back we showed you how to "fake" a dialog , which is pretty cool. We ended up using this technique a few times until eventually we asked ourselves: "Shouldn't this be a part of the framework?".

Well, now it is. We just added InteractionDialog to Codename One which tries to be very similar to Dialog in terms of API but unlike dialog it never blocks anything. Not the calling thread or the UI.
Its really just a container that is positioned within the layered pane. Notice that because of that design you can have only one such dialog at the moment and if you add something else to the layered pane you might run into trouble.

Using the interaction dialog is be pretty trivial and very similar to dialog:
                final InteractionDialog dlg = new InteractionDialog("Hello");
                dlg.setLayout(new BorderLayout());
                dlg.addComponent(BorderLayout.CENTER, new Label("Hello Dialog"));
                Button close = new Button("Close");
                close.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {
                        dlg.dispose();
                    }
                });
                dlg.addComponent(BorderLayout.SOUTH, close);
                Dimension pre = dlg.getContentPane().getPreferredSize();
                dlg.show(0, 0, Display.getInstance().getDisplayWidth() - (pre.getWidth() + pre.getWidth() / 6), 0);

This will show the dialog on the right hand side of the screen, which is pretty useful for a floating in place dialog.

Notice: This post was automatically converted using a script from an older blogging system. Some elements might not have come out as intended.... If that is the case please let us know via the comments section below.

Share this Post:

Posted by Shai Almog

Shai is the co-founder of Codename One. He's been a professional programmer for over 25 years. During that time he has worked with dozens of companies including Sun Microsystems.
For more follow Shai on Twitter & github.