This discussion is archived
4 Replies Latest reply: Oct 13, 2012 7:22 AM by TPD-Opitz-Consulting-com RSS

Sending a text between JTextField controls of two JFrames

616286 Newbie
Currently Being Moderated
Hi
I have two classes that extends JFrame class , the first class (NewJFrame) contains JButton and a JTextField
as well as the second class (NewJFrame1) contains
The problem is I want to enter a text on the JTextField control of the first jframe that should be appear on the JTextField of the
second JFrame after pressing it's jbutton.
After the jbutton of the the first jframe pressed
1-the NewJFrame1 should appear I have done it by new NewJFrame1().setVisible(true);
2-The Text which has entered int the first jFrame should appear when pressing the jbutton of the second jframe

However this is my trial by NetBeans
 
public class NewJFrame extends javax.swing.JFrame {

   public static  String NickName;
    public NewJFrame() {
        initComponents();
      NickName = jTextField1.getText().trim();

    }

       public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }

    @Action
    public void gfg() {
         
        new NewJFrame1().setVisible(true);
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration                   

}
My second jframe
 

 
public class NewJFrame1 extends javax.swing.JFrame {

    /** Creates new form NewJFrame1 */
    public NewJFrame1() {
        initComponents();
    }

  
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

 
        setName("Form");  

  

       
     } 

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame1().setVisible(true);
            }
        });
    }

    @Action
    public void getNickName() {
      
      jTextField1.setText(NewJFrame.NickName);
       
    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration

}
Edited by: VANPERSIE on Oct 8, 2012 10:46 AM
  • 1. Re: Sending a text between JTextField controls of two JFrames
    morgalr Explorer
    Currently Being Moderated
    If I understand your need correctly:
    You need a second JFrame to get the contents of a JTextField on the first JFrame whan a button is pressed:
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class JFrameJunk {
        private MyJFrameA A;
        private MyJFrameB B;
      public JFrameJunk(){
        A = new MyJFrameA();
        B = new MyJFrameB(A);
      }
      public static void main(String[] args) {
        new JFrameJunk();
      }
      public class MyJFrameA extends JFrame{
          private JTextField t;
        public MyJFrameA(){
          this.setTitle("JFrame A");
          this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          JPanel p = new JPanel();
          t = new JTextField();
          t.setPreferredSize(new Dimension(250, 20));
          JButton b = new JButton("OK");
          b.addActionListener(new MyActionListener());
          
          p.add(t);
          p.add(b);
          this.add(p);
          this.pack();
          this.setVisible(true);
        }
        public class MyActionListener implements ActionListener{
          public MyActionListener(){
            
          }
          public void actionPerformed(ActionEvent e){
            A.setVisible(false);
            B.setVisible(true);
          }
        }
      }
      public class MyJFrameB extends JFrame{
        private JTextField t;  
        public MyJFrameB(MyJFrameA A){
            this.setTitle("JFrame B");
          this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          JPanel p = new JPanel();
          t = new JTextField();
          t.setPreferredSize(new Dimension(250, 20));
          JButton b = new JButton("OK");
          b.addActionListener(new MyActionListener());
          
          p.add(t);
          p.add(b);
          this.add(p);
          this.pack();       
        }
        public class MyActionListener implements ActionListener{
          public MyActionListener(){
            
          }
          public void actionPerformed(ActionEvent e){
            B.t.setText(A.t.getText());
          }
        }
      }
    }
  • 2. Re: Sending a text between JTextField controls of two JFrames
    840135 Newbie
    Currently Being Moderated
    Where is the action handler code for the button? The code which displays the text on the second frame should be written inside the event handler of the first frame which displays the second frame.
  • 3. Re: Sending a text between JTextField controls of two JFrames
    morgalr Explorer
    Currently Being Moderated
    MyActionListener local to each custom JFrame.

    Edited by: morgalr on Oct 11, 2012 1:45 PM
  • 4. Re: Sending a text between JTextField controls of two JFrames
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    VANPERSIE wrote:
    The problem is I want to enter a text on the JTextField control of the first jframe that should be appear on the JTextField of the
    second JFrame after pressing it's jbutton.
    After the jbutton of the the first jframe pressed
    1-the NewJFrame1 should appear I have done it by new NewJFrame1().setVisible(true);
    2-The Text which has entered int the first jFrame should appear when pressing the jbutton of the second jframe
    Just to have that mentionned:
    Your approach is not suitable for a chat programm working over a network...

    bye
    TPD

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points