Makusium
Makusium

Reputation: 367

Custom dialog won't dispose when dispose is called

So I'm very new to Codename One and I was trying out the Dialog class. When I create the Dialog it shows the Buttons and the text but when I want to dipose it, it won't disappear. Any suggestions?

private void createDialog(String title) {

    Dialog dialog = new Dialog(title);
    dialog.add("You ended the game.");
    dialog.add("Choose what to do");
    Button stay = new Button("Stay");
    Button newGame = new Button("New Game");
    Button menu = new Button("Menu");

    dialog.add(stay);
    dialog.add(menu);
    dialog.add(newGame);
    dialog.show();
    dialog.dispose();

     if(stay.isToggle()){
        //
     }

  }

I'm expecting a dialogbox with 3 buttons, 2 text. Then to dispose when I call the method. Also another question is the isToogle the right method to call, when I want something to happen if I click on the button?

Upvotes: 0

Views: 82

Answers (1)

Diamond
Diamond

Reputation: 7483

You are calling dispose() method immediately after show(), which is wrong as it will attempt to dispose the dialog right after showing it.

Also, to add click event to a button, call addActionListener().

private void createDialog(String title) {

    Dialog dialog = new Dialog(title);
    dialog.add("You ended the game.");
    dialog.add("Choose what to do");
    Button stay = new Button("Stay");
    Button newGame = new Button("New Game");
    Button menu = new Button("Menu");

    dialog.add(stay);
    dialog.add(menu);
    dialog.add(newGame);
    dialog.show();

    stay.addActionListener(evt -> {
         dialog.dispose();
    });
}

Upvotes: 2

Related Questions