Results 1 to 2 of 2

Thread: Passing info between JFrames...

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2002
    Posts
    45

    Passing info between JFrames...

    Hello,

    I have created to JFrames. I want to pass information from 2 textboxes in one to 2 corresponding boxes in the other JFrame. I am not sure at all how to go about this? I am an experienced VB coder, but have to develop in java.

    I understand I would have to use a get/set object?

    What kind of code would I put in the sending JFrame and what kind in the receiving JFrame?

    Any help is greatly appreciated.

    -Mike

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    The code below would work ok but only to send. If the values changed after sent then you could always send them back to the original source. If you wanted to implement a drag and drop scheme that would be another story.
    Code:
    import java.awt.*;
    import javax.swing.*; 
    import java.awt.event.*; 
    
    public class F{
     public static void main(String[] args){
       JFrame sending = new JFrame("Sending");
       JFrame receiving = new JFrame("Receiving");
       JButton jb = new JButton("Send");
    
       Container s = sending.getContentPane();  
       Container r = receiving.getContentPane();   
    
       JTextField js = new JTextField(10);
       JTextField jr = new JTextField(10);
     
       sending.addWindowListener(new WindowAdapter(){
        public void widowClosing(WindowEvent evt){
           System.exit(0);
        }
       });
    
       receiving.addWindowListener(new WindowAdapter(){
        public void widowClosing(WindowEvent evt){
           System.exit(0);
        }
       });
    
       jb.addActionListener(new Sender(js,jr));
       
       s.add(js,BorderLayout.NORTH);
       s.add(jb,BorderLayout.SOUTH); 
       r.add(jr,BorderLayout.NORTH);
    
       sending.setSize(250,200); 
       receiving.setSize(250,200);
       
       sending.setVisible(true);
       receiving.setVisible(true);
     
     }
    }
    
    class Sender implements ActionListener{
      JTextField js; 
      JTextField jr;  
    
      public Sender(JTextField js,JTextField jr){
        this.js = js; 
        this.jr = jr;
      }
    
      public void actionPerformed(ActionEvent evt){ 
           jr.setText(js.getText());
       }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width