-1

How it works?

  1. Class MiWindows is responsible for running the class MiOtherWindows.
  2. MiOtherWindows class interacts with the user who changes their status and closes.
  3. Class MiWindows detects the change of value in the class MiOtherWindows.
  4. The Class MiWindows prints out the changes.

What is the Problem? MiWIndows class does not detect the change in the child class MiOtherWindows.

Question?

How to make a window I can listen to another without using threads?

//-------------------------------------------------------------------

//HERE IS THE CODE OF MiWindows

public class MiWindows extends JDialog {

    private JButton doing = new JButton("The Button");

    public MiWindows(SeconWindows owner) {

        doing.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                Object value;
                try {
                    value = new MiOtherWindows(MiWindows.this).getValue();
                    if (value != null) {
                        System.out.println("Never Print this...");
                    }
                } catch (Exception x) {
                    System.out.println("exception");
                }
            }
        });
    }
}

//-------------------------------------------------------------------

//HERE IS THE CODE OF MiOtherWindows

public class MiOtherWindows extends JDialog {

    private JButton close = new JButton("The Button");
    private String value = null;

    public MiOtherWindows(JDialog owner) {
        super(owner);
        //super(owner, true); modal don't work

        close.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                this.value = "This is Result";
                setVisible(false);
            }
        });
    }

    public String getValue() {
        return value;
    }
}
Gino
  • 33
  • 4
  • 1
    I see nowhere where you use a WindowListener. The listening can't happen by magic of course. Solution -- add a listener to the listened-to window so you can respond to its closing. – Hovercraft Full Of Eels May 21 '12 at 16:38

1 Answers1

0

Change definition of constructor from

public VentanaCrearExamen(JDialog owner)

to

public VentanaCrearExamen(MiWindows owner)

Store the reference to MiWindows in MiWindow:

private MiWindows owner;
.....
public VentanaCrearExamen(MiWindows owner) {
    this.owner = owner;
}

Define special callback method in MiWindows, for example

public void childActionPerformed(ActionEvent e) {
}

Now when any event happens in MiWindow you can call call-back method to MiWindows

   close.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            this.value = "This is Result";
            setVisible(false);
            owner.childActionPerformed(e);
        }
    });

A lot of other solutions are also available. I hope this tip will help you and will probably give you other ideas.

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • AlexR thank you very much! Your reply works perfect. I try to use the observer pattern, but I can not use threads of execution. How I can build an algorithm to solve this without losing elegance? – Gino May 21 '12 at 18:51