3

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

Jad Chahine
  • 6,849
  • 8
  • 37
  • 59
Ashan
  • 479
  • 7
  • 28
  • 1
    use a call back interface between those 2 jframes... – ΦXocę 웃 Пepeúpa ツ Mar 04 '16 at 08:48
  • 1
    Use [JOptionPane.showInputDialog(...)](https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html) – Sergiy Medvynskyy Mar 04 '16 at 08:49
  • @Ashan Another JFrame from where you have created ? can you post little bit more code so that we can help you ? – Vishal Gajera Mar 04 '16 at 08:52
  • at this situation jOptionPane not a solution @SergiyMedvynskyy :( thank you – Ashan Mar 04 '16 at 08:53
  • this is same project. when open button click then jframe2 opening. when type some text and click ok button. jframe2 dispose and value must me set jframe1 lable. not closing jframe1 when click open. – Ashan Mar 04 '16 at 08:58
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) As suggested by @SergiyMedvynskyy, an option pane input dialog seems well suited to replace the 2nd frame. – Andrew Thompson Mar 04 '16 at 09:00
  • how to use call back @Xoce please give me useful example link. thank you – Ashan Mar 04 '16 at 09:01
  • *"at this situation jOptionPane not a solution"* ..why? – Andrew Thompson Mar 04 '16 at 09:01
  • I have different project. it's using little complex two jframes. because why jOptionPane can't handle my problem i think. @AndrewThompson – Ashan Mar 04 '16 at 09:05
  • 1
    Use a modal dialog, which will block your codes execution until the window is closed, at which time you can use a getter to get the value from the other class – MadProgrammer Mar 04 '16 at 09:06

3 Answers3

3

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

Community
  • 1
  • 1
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
2

Try This

enter image description here

import java.awt.FlowLayout;
import javax.swing.*;
import java.awt.event.*;

class TestFrameExample extends JFrame  implements ActionListener{
    static JLabel label ; 
    public static TestFrameExample test;
    TestFrameExample()
   {
      JPanel panel = new JPanel();
      panel.setLayout(new FlowLayout());
      label = new JLabel("This is a label!");
      JButton button = new JButton("Open");
      button.setText("Press me");
      button.addActionListener(this);
      panel.add(label);
      panel.add(button);
      add(panel);
      setSize(300, 300);
      setLocationRelativeTo(null);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);
  }

   public void actionPerformed(ActionEvent a)
   {
          new TestFrameExample1();
  }
  public static void main(String s[]) {
      test=new TestFrameExample();
  }
}

class TestFrameExample1 extends JFrame  implements ActionListener {
  JTextField t;
  TestFrameExample test;
  public TestFrameExample1()
  {
        setSize(300, 300);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setLayout(null);
        t=new JTextField();
        t.setBounds(100,20,150,20);
        JButton button=new JButton("oK");
        button.setBounds(100,50,100,30);
        button.addActionListener(this);
        add(t);
        add(button);
    }
    public void actionPerformed(ActionEvent a)
   {
        test.label.setText(t.getText());
   }
  }
Benjamin
  • 2,257
  • 1
  • 15
  • 24
0

create a method that takes jframe1 in the jframe2 in the open button action event create a object from jframe2 and call that method that take jframe1.

so when u click Ok button in the jframe2 pass that text field value to the jframe1 object (that u passed to the jframe2) via a methdo

public class jframe1 {

  public void actionPerformed(ActionEvent a){
     jfame2 jf2 = new jframe2();
     jf2.setJframe1(this);
  }
  public void updateLable(String value){
     lblIdk.setText(value);
  }
}


public class jframe2 {
  private jframe1 jf1;
  public void setJframe1(jframe1 jf1){
     this.jf1 = jf1;
  }
  public void actionPerformed(ActionEvent a){
     this.jf1.updateLable(txtidk.getText());
  }
}
Keaz
  • 955
  • 1
  • 11
  • 21