3

I've got a JDialog bound to my (separate) controller class via beans binding (Netbeans). My dialog has a "close" Button. The action property of this button is bound to an action in my controller.

Dialog:

public class AppVersionCheckDialog extends javax.swing.JDialog {
    ...
    binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, controller, org.jdesktop.beansbinding.ELProperty.create("${closeButtonActionListener}"), btnOk, org.jdesktop.beansbinding.BeanProperty.create("action"));
    bindingGroup.addBinding(binding);
    ...
}

So basically I got

public class AppVersionCheckDialogController extends AbstractController {

    private final Action closeAction = new AbstractAction("Close") {
        @Override
        public void actionPerformed(ActionEvent e) {
            // dialog.dispose() - no reference of dialog instance here 
        }
    };


    public Action getCloseButtonActionListener(){
        return closeAction;
    }
}

in my controller.

I do not have any reference to the dialog within the controller. And I don't want to introduce one, as it breaks the whole principle of binding things together.

So how to close the dialog? Is there a way to bind the dialog instance to a property of my controller? If so, how?

gorootde
  • 4,003
  • 4
  • 41
  • 83

1 Answers1

7

I don't have a compiler with me now, but if I understand correctly what you want is similar to this:

public void actionPerformed(ActionEvent e) {
        Component component = (Component) e.getSource();
        JDialog dialog = (JDialog) SwingUtilities.getRoot(component);
        dialog.dispose();
      }

I'll have a look when I have the chance to compile and if it has problems. I hope, it solves your problem.

halil
  • 800
  • 16
  • 33
  • The above solution is not equivalent to clicking the close window button. It does not run the event listener for windowClosing(...) – Queeg Oct 31 '21 at 12:25