Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass value between two jframes

Tags:

java

swing

I have two jframes, I want to get value from opened another jframe to other opened jframe. when click jframe1 open button showing jframe2 and type some text in text field and click ok button, text field value want to get jframe1 jlable. how to do this i tried but i can't find a way to do this.

Is this possible ?

enter image description here

enter image description here

like image 589
Ashan Avatar asked May 17 '26 05:05

Ashan


1 Answers

Use a callback,

add this code to your project:

Define an interface

public interface ICallbackListener{
    void onNewEvent(String msg);
}

add to jframe 2:

private ICallbackListener myListener;
public void addCallback(ICallbackListener myListener){
    this.myListener = myListener;
}


...
if(myListener!=null){
myListener.onNewEvent("myMessage");
}
...

add to jframe 1:

private ICallbackListener myListener;

ICallbackListener i = new ICallbackListener() {
            
            @Override
            public void onNewEvent(String msg) {
                // TODO Auto-generated method stub
                
            }
        };
        
public void setCallback( ){
    jframe2.addCallback(myListener);
}

now, every thime the jframe2 call the interface method you will get asynchronous a call to the TODO label in the jframe1

like image 189
ΦXocę 웃 Пepeúpa ツ Avatar answered May 18 '26 18:05

ΦXocę 웃 Пepeúpa ツ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!