Close "IDialogHandler" programmatically

Hey,
I think this is a selly question but I really can’t find an answer.
I have a class that implements IDialogHandler:

public class ClassDiagramStereo implements IDialogHandler {

    JButton OkButton = new JButton("Ok");
	JTextField stereoTextField = new JTextField();

    @Override
    public Component getComponent() {
        JPanel mainPane = new JPanel();

        mainPane.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(2, 2, 2, 2);


        //Ok Button
		mainPane.add(OkButton, gbc);
        mainPane.add(stereoTextField, gbc);

        // Ok Button
        OkButton.addActionListener(e -> {
          //Do some thing...
		  //Then close the window
        });

         
        return mainPane;
    }

    @Override
    public void prepare(IDialog dialog) {
        dialog.setModal(true);
        dialog.setTitle("MDE - Select PDM");
        dialog.setResizable(true);
        dialog.pack();
    }

    @Override
    public void shown() {
        
    }

    @Override
    public boolean canClosed() {
        return true;
    }

    public string getStereo(){
        return stereoTextField.getText();
    }

}

When pressing the button “Ok” i want to do some work then the window closes.

i call the ClassDiagramStereo handler from another handler like this:

ViewManager vm = ApplicationManager.instance().getViewManager();
ClassDiagramStereo classDiagramStereo= new ClassDiagramStereo();
vm.showDialog(classDiagramStereo);
Stereo = classDiagramStereo.getStereo();

My question is how I can close programmatically an “IDialogHandler” ?

Thank you

The instance of IDialog you get on the prepare(IDialog dialog) method has a close() method that you can use (see the APU here).

Just remember to invoke the dialog on its own thread.

1 Like

This is how i managed to close the window (Dialog)

// Ok Button
OkButton.addActionListener(e -> {
    //Do some thing...
    Component component = (Component) e.getSource();
    JDialog dialog = (JDialog) SwingUtilities.getRoot(component);
    dialog.dispose(); 
});