1

I have created an array Man:

public main blah blah{
man = man[10];
}

Man has fields such as

Man.name;
Man.age;
...

In Man class, there is a OnClick method that opens a new window showing its name and age.

public Man(){

    Onclick(){
        InfoWindow showinfo = new InfoWindow(this.getid()) // If this is Man[2] the id would be 2.

}

And in InfoWindow class:

public class InfoWindow extends JFrame{
    public InfoWindow(Man selectedMan){
        setSize(300, 200);
        JLabel info = new JLabel(selectedMan.getname());
        add(info);
        info.setVisible(true);
     }
}

Basically, that's wanna acomplish (show in pseudocode), pass a Man[i] into a class that when a window is created, shows the info related to that man. This is how i'm actualy trying to implement it but it's not working, i'm pretty sure there is a misconception from me in some part.

Any help?

Actual code:

***MAN CLASS***
private class MouseListenerHandler extends MouseAdapter {
        public void mousePressed(MouseEvent e) {
            InfoWindow manShowInfo = new InfoWindow(this); Not Working. Getting "constructor not defined"
            unitShowInfo.setVisible(true);

        }
    }

*InfoWindow class*
public class InfoWindow extends JFrame {
    public InfoWindow(Man selectedMan){
        setSize(300, 200);
        JLabel label = new JLabel(selectedMan.getName());
        add(label);
        label.setVisible(true);

    }

And the Man[] is created in the main class.
}
1
  • 2
    A description of the symptoms would help. Is it not compiling? Is it throwing an exception (which exception)? Is it just failing silently? Something else? Commented Apr 18, 2010 at 1:47

1 Answer 1

4

Try this:

InfoWindow manShowInfo = new InfoWindow(Man.this);

Because the event listener is itself an object instance, a plain this refers to the listener. Doing Man.this will extract the enclosing Man instance to pass into the InfoWindow.

Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.