Skip to main content

Close Dialog using buttons

6 replies [Last post]
bjovesom
Offline
Joined: 2008-07-17

Hi

I was wondering if if it is possible to close a Dialog using Buttons instead of commands. I have created a container containing buttons and the dialog shows them perfectly. As Command[] (in parameter list to Dialog.show(..)), I send null. Have anyone tried this?
Thanks in advance!

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Shai Almog

Hi,
sure you can. In the button listener just invoke Dialog.dispose();

Thanks,
Shai.

> Hi
>
> I was wondering if if it is possible to close a Dialog using
> Buttons instead of commands. I have created a container containing
> buttons and the dialog shows them perfectly. As Command[] (in
> parameter list to Dialog.show(..)), I send null. Have anyone tried
> this?
> Thanks in advance!
> [Message sent by forum member 'bjovesom' (bjovesom)]
>
> http://forums.java.net/jive/thread.jspa?messageID=287377
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
> For additional commands, e-mail: users-help@lwuit.dev.java.net
>

Shai Almog
http://lwuit.blogspot.com/

[att1.html]

bjovesom
Offline
Joined: 2008-07-17

I tried it, but it does not work for me. Have you tried it and got it to work?

Shai Almog

I'm guessing your addActionListener is after the show() method...
This works:
Form frm = new Form("Form");
Button btn1 = new Button("Show Dialog");
frm.addComponent(btn1);
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
final Dialog dlg = new Dialog("Dialog");
Button close = new Button("Close");
dlg.addComponent(close);
close.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent
evt) {
dlg.dispose();
}
});
dlg.show();
}
});
frm.show();


> I tried it, but it does not work for me. Have you tried it and got
> it to work?
> [Message sent by forum member 'bjovesom' (bjovesom)]
>
> http://forums.java.net/jive/thread.jspa?messageID=287384
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
> For additional commands, e-mail: users-help@lwuit.dev.java.net
>

Shai Almog
http://lwuit.blogspot.com/

[att1.html]
[Picture 76.png]

bjovesom
Offline
Joined: 2008-07-17

Thanks for the fast reply.

I found the problem, but I am not sure why it was a problem.
Earlier I called dlg.show("Operations", container, null), where container had my buttons and stuff. The dialog displayed perfectly, but dispose didnt work. I changed it to dlg.show() and the dispose suddenly works. It sounds weird for me, is it a bug?

Shai Almog

Hi,
show has several varieties some of which are static and a few of
which are not.
show() is not static and so dlg.show() will show the dlg dialog.

However Dialog.show(String, Component, Commands[]) is static and will
create a new dialog... So you disposed a dialog that was never shown...

To solve this your button can do something like this:

Dialog d = (Dialog)Display.getInstance().getCurrent();
d.dispose();

Thanks,
Shai.

> Thanks for the fast reply.
>
> I found the problem, but I am not sure why it was a problem.
> Earlier I called dlg.show("Operations", container, null), where
> container had my buttons and stuff. The dialog displayed perfectly,
> but dispose didnt work. I changed it to dlg.show() and the dispose
> suddenly works. It sounds weird for me, is it a bug?
> [Message sent by forum member 'bjovesom' (bjovesom)]
>
> http://forums.java.net/jive/thread.jspa?messageID=287392
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
> For additional commands, e-mail: users-help@lwuit.dev.java.net
>

Shai Almog
http://lwuit.blogspot.com/

[att1.html]

bjovesom
Offline
Joined: 2008-07-17

Youre absolutely right. I guess it was to late for me yesterday... :)

Thanks for the solution!