简体   繁体   中英

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 mycustompanel s 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?

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM