Results 1 to 7 of 7
Like Tree2Likes
  • 2 Post By DarrylBurke

Thread: Send a text into another JFrame

  1. #1
    Jhovarie is offline Banned
    Join Date
    Feb 2011
    Posts
    65
    Rep Power
    0

    Default Send a text into another JFrame

    Hello guys plss ... help im getting error here
    i have a 2 java file name JFrameA and JFrameB

    now JFrameA has a textarea and jbutton then heres what iwant to do
    if i write a text from textarea of JFrameA then click the button i want to send it
    on the textArea of JFrameB and thesame with JFrameB i want to send the text from textarea of JFrameB to the textArea of JFrameA.

    see my codes

    //this is JFrameA.java
    Java Code:
    public class JFrameA extends javax.swing.JFrame {
        /** Creates new form NewJFrame */
        public JFrameA(){
            initComponents();
        }
    
        JFrameA(String text) {
            jTextArea1.setText(text);
        }
        @SuppressWarnings("unchecked")
       //Generated Code
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    new JFrameB(""+jTextArea1.getText());
        }


    //this is JFrameB.java
    Java Code:
    public class JFrameB extends javax.swing.JFrame {
    
        public JFrameB() {
            initComponents();
        }
    
        JFrameB(String strtext) {
            jTextArea1.setText(""+strtext);
        }
        @SuppressWarnings("unchecked") 
       //Generated Code
    
     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    new JFrameA(""+jTextArea1.getText()); // TODO add your handling code here:
        }

    then this is my error message

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at codingerror.JFrameB.<init>(JFrameB.java:25)
    at codingerror.JFrameA.jButton1ActionPerformed(JFrame A.java:73)
    at codingerror.JFrameA.access$000(JFrameA.java:18)
    at codingerror.JFrameA$1.actionPerformed(JFrameA.java :40)

    thanks

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You need some way for them to access each other. The easiest way to do this is to give them each a reference to the other type as an instance variable.

    A should have a instance variable for B, and B should have an instance variable for B, then when you click on A's button it can manipulate it's reference to B and vice versa for B's button.

    Also your error is related to something that we can't tell from your snippets. The error means you haven't instantiated something. Use the line numbers in the stack trace to locate what is callig a method on a null object.

    You also shouldn't be supressing warnings unless you know what is being supressed.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    You also shouldn't be supressing warnings unless you know what is being supressed.
    That's part of the horrible, and thankfully not posted, NetBeans visual designer's generated code.

    @Jhovarie
    1. Drop the visual designer,it's not a beginners' tool.
    2. You don't 'send a text' anywhere. You pass a String as a parameter to a constructor or method invocation.
    3. Most UI designs don't require multiple JFrames. In general, there should be only one JFrame as the main application GUI, andother windows should be JDialogs, usually modal.
    4. To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem. And write the code yourself -- don't post unreadable autogenerated code.

    db
    Fubarable and Norm like this.

  4. #4
    Jhovarie is offline Banned
    Join Date
    Feb 2011
    Posts
    65
    Rep Power
    0

    Default

    this one works.

    import javax.swing.*;
    import java.awt.event.*;
    public class Main{
    public static void main(String[]args){
    JButton btn1 = new JButton("Submit to Form2");
    final JTextArea txtArea1 = new JTextArea(10,20);
    JButton btn2 = new JButton("Submit to Form1");
    final JTextArea txtArea2 = new JTextArea(10,20);
    btn1.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent evt){
    txtArea2.setText(txtArea1.getText());
    }
    });

    btn2.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent evt){
    txtArea1.setText(txtArea2.getText());
    }
    });
    JFrame frm1 = new JFrame("Form 1");
    frm1.add(txtArea1);
    frm1.add(btn1);
    frm1.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));
    frm1.setBounds(50,10,400,300);
    frm1.setDefaultCloseOperation(3);
    frm1.setVisible(true);

    JFrame frm2 = new JFrame("Form 2");
    frm2.add(btn2);
    frm2.add(txtArea2);
    frm2.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));
    frm2.setBounds(410,10,400,300);
    frm2.setDefaultCloseOperation(3);
    frm2.setVisible(true);
    }
    }

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Please use code tags so it maintains formatting an we can read it easily. Also, you shouldn't be explicitly stating a number for setDefaultCloseOperation, instead you should be using the constants, such as JFrame.EXIT_ON_CLOSE.

    Also, if you are going to use two JFrames, I suggest you make them separate classes and pass references in the constructor

    Java Code:
    public class FirstClass extends JFrame{
      JFrame second;
      public FirstClass(){/*...*/)
      //other stuff as necessary
    }
    Last edited by sunde887; 07-12-2011 at 04:54 PM.

  6. #6
    Jhovarie is offline Banned
    Join Date
    Feb 2011
    Posts
    65
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    Please use code tags so it maintains formatting an we can read it easily. Also, you shouldn't be explicitly stating a number for setDefaultCloseOperation, instead you should be using the constants, such as JFrame.EXIT_ON_CLOSE.

    Also, if you are going to use two JFrames, I suggest you make them separate classes and pass references in the constructor

    Java Code:
    public class FirstClass extends JFrame{
      JFrame second;
      public FirstClass(){/*...*/)
      //other stuff as necessary
    }

    Ok im trying this code and i got some error this is my code.

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    public class Frame1 extends JFrame{
    private Frame2 frm2;
    private JTextArea textA;
    private JButton btn;
    public Frame1(){
    textA = new JTextArea(7,10);
    textA.requestFocusInWindow();
    frm2 = new Frame2();
    btn = new JButton("Submit");
    btn.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            frm2.setTextVoid(textA.getText());
        }
    });
    add(textA);
    add(btn);
    setLayout(new java.awt.FlowLayout());
    setBounds(10,10,300,200);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
    frm2.setVisible(true);
    }
    public static void main(String[]args){
     new Frame1();
    }
        void setTextVoid(String text) {
        textA.setText(text);
        }
    }
    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    public class Frame2 extends JFrame{ 
        JButton btn;
    JTextArea textB;
    private Frame1 frm1 = new Frame1();
    public Frame2(){;
        btn = new JButton("Submit");
        btn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                  frm1.setTextVoid(textB.getText());
                }
            });
        add(btn);
    textB = new JTextArea(7,10);
    add(textB);    
    setLayout(new java.awt.FlowLayout());   
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setBounds(400,10,300,200);
    setVisible(true);
    }
    public void setTextVoid(String str){
        textB.setText(str);
    }
    }
    and this is the error message

    Exception in thread "main" java.lang.StackOverflowError
    at sun.awt.AppContext.get(AppContext.java:580)
    at sun.awt.SunToolkit.getSystemEventQueueImplPP(SunTo olkit.java:1124)
    at sun.awt.SunToolkit.getSystemEventQueueImplPP(SunTo olkit.java:1120)
    at sun.awt.SunToolkit.getSystemEventQueueImpl(SunTool kit.java:1115)
    at java.awt.Toolkit.getEventQueue(Toolkit.java:1689)
    at java.awt.EventQueue.isDispatchThread(EventQueue.ja va:807)
    at javax.swing.SwingUtilities.isEventDispatchThread(S wingUtilities.java:1339)
    at javax.swing.text.StyleContext.reclaim(StyleContext .java:437)
    at javax.swing.text.StyleContext.addAttributes(StyleC ontext.java:322)
    at javax.swing.text.AbstractDocument$AbstractElement. addAttributes(AbstractDocument.java:1971)
    at javax.swing.text.AbstractDocument$AbstractElement. <init>(AbstractDocument.java:1762)

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Jhovarie View Post
    Ok im trying this code and i got some error this is my code.

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    public class Frame1 extends JFrame{
    private Frame2 frm2;
    private JTextArea textA;
    private JButton btn;
    public Frame1(){
    textA = new JTextArea(7,10);
    textA.requestFocusInWindow();
    frm2 = new Frame2();
    btn = new JButton("Submit");
    btn.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            frm2.setTextVoid(textA.getText());
        }
    });
    add(textA);
    add(btn);
    setLayout(new java.awt.FlowLayout());
    setBounds(10,10,300,200);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
    frm2.setVisible(true);
    }
    public static void main(String[]args){
     new Frame1();
    }
        void setTextVoid(String text) {
        textA.setText(text);
        }
    }
    ACK... MY EYES!

    Surely you must see that your code is formatted poorly and very difficult to read. Please compare your first class with this:

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    
    public class Frame1 extends JFrame {
       private Frame2 frm2;
       private JTextArea textA;
       private JButton btn;
    
       public Frame1() {
          textA = new JTextArea(7, 10);
          textA.requestFocusInWindow();
          frm2 = new Frame2();
          btn = new JButton("Submit");
          btn.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                frm2.setTextVoid(textA.getText());
             }
          });
          add(textA);
          add(btn);
          setLayout(new java.awt.FlowLayout());
          setBounds(10, 10, 300, 200);
          setDefaultCloseOperation(EXIT_ON_CLOSE);
          setVisible(true);
          frm2.setVisible(true);
       }
    
       public static void main(String[] args) {
          new Frame1();
       }
    
       void setTextVoid(String text) {
          textA.setText(text);
       }
    }
    and tell me which is easier to read. We appreciate it greatly if you take the time and effort to format your posted code properly so as to make them easy to read, especially since you're asking the volunteers here to take the time and effort to try to help you.

    Anyway as to your problem, when you see a stackoverhead issue think recursion gone wild as the likely cause, and sure enough that's what's happening. Picture in your mind's eye what this distillation of your code will do:

    Java Code:
    public class Frame1 {
       private Frame2 frm2;
    
       public Frame1() {
          frm2 = new Frame2();
       }
    
       public static void main(String[] args) {
          new Frame1();
       }
    
    }

    Java Code:
    public class Frame2 {
       private Frame1 frm1 = new Frame1();
    
    }
    So to walk through this with you, in main you create a Frame1 object which in its constructor creates a Frame2 object which when created also creates a Frame1 object, which in its constructor creates a Frame2 object which when created also creates a Frame1 object, which in its constructor creates a Frame2 object which when created also creates a Frame1 object, which in its constructor creates a Frame2 object which when created also creates a Frame1 object, which in its constructor creates a Frame2 object which when created also creates a Frame1 object, which in its constructor creates a Frame2 object which when created also creates a Frame1 object, ... ad infinitum or until memory runs out.

    A solution is to create your Frame1 and Frame2 objects in main and pass reference of one to the other. For instance:

    Java Code:
    public class Frame1 {
       private Frame2 frm2;
    
       public Frame1() {
          //frm2 = new Frame2();
       }
       
       public void setFrame2(Frame2 frm2) {
          this.frm2 = frm2;
       }
    
       public static void main(String[] args) {
          Frame1 frm1 = new Frame1();
          Frame2 frm2 = new Frame2();
          
          frm1.setFrame2(frm2);
          frm2.setFrame1(frm1);
          
       }
    }
    
    class Frame2 {
       //Frame1 frm1 = new Frame1();
       Frame1 frm1;
       
       public void setFrame1(Frame1 frm1) {
          this.frm1 = frm1;
       }
    }
    Last edited by Fubarable; 07-13-2011 at 10:50 PM.

Similar Threads

  1. send and receive text messages
    By dking in forum Networking
    Replies: 1
    Last Post: 06-21-2011, 11:53 AM
  2. how do i use jframe button to send input selection
    By earnest in forum New To Java
    Replies: 5
    Last Post: 04-04-2011, 08:46 PM
  3. send text msg from pc to mobile
    By nyk976 in forum Networking
    Replies: 0
    Last Post: 03-21-2010, 03:03 PM
  4. Replies: 4
    Last Post: 11-26-2009, 09:40 AM

Tags for this Thread

Posting Permissions

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