geo
geo

Reputation: 527

Return value from custom swing components to parent class

I have the main class named myapp of my applicaion that has a jLayeredPane.

I have a class named mycustompanel that extends jpanel.

In myapp I have a method that adds several mycustompanel objects in the jLayeredPane. The number of mycustompanels added is not fixed.

The mycustompanel has a property named personsurname that is given a value when mycustompanel is added to the jLayeredPane.

mycustompanel has this listener:

    @Override
    public void mouseClicked(MouseEvent evt){
    if(evt.getButton() == MouseEvent.BUTTON1){
    answer=personsurname;
    JOptionPane.showMessageDialog(null,answer);    
    }  
    }

where answer is defined as a public String. So I can see the value of the personsurname of the clicked mycustompanel.

What I want is to get the answer value to myapp.

I think I should add a method like

public String getAnswer() { return answer; }

to the mycustompanel , but how do I call this method from the main class, so that each time a mycustompanel is clicked the personsurname value of the specific item that is clicked gets to the myapp class?

Upvotes: 0

Views: 73

Answers (1)

tr4pt
tr4pt

Reputation: 317

Hmm as far as I understand it you could write a setPersonSurnameInMyapp method in MyApp. so everytime the mouseevent is called you call that method and save the value in a variable in Myapp?

Upvotes: 1

Related Questions