Results 1 to 9 of 9
  1. #1
    rippon is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default trying to communicate between classes

    I'm trying to communicate from one class to another, but just can't manage it. I hope the following code is self-explanatory about what I'm trying to do.

    Thanks in advance for any replies.
    Java Code:
    public class SuperPanel extends JPanel {
       public JTextField messageBox = new JTextField("public message box", 20);
       private SubPanel mySP = new SubPanel();
    
        public SuperPanel() {
          add(messageBox);
          add(mySP);
        } // end of SuperPanel constructor
    
    } // end of: public class SuperPanel extends JPanel
    
    
    public class SubPanel extends JPanel{
    
       public SubPanel(){ // I have tried the following options
    // to try to communicate with the SuperPanel class
          messageBox.setText("hallo from SubPanel"); //option 1
          super.messageBox.setText("hallo from SubPanel"); //option 2
      } // end of SubPanel() constructor
    
    } // end of SubPanel class

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Just like the SuperPanel class has a reference to the subPanel class the reverse has to be true too. How can SubPanel communicate to SuperPanel if it doesn't have any references to a SuperPanel? Here's one way to fix it.
    Java Code:
    public class SuperPanel extends JPanel {
        private SubPanel mySP = new SubPanel(this); // passes a reference of the SuperPanel to the SubPanel constructor
    
        public SuperPanel() {
            add(messageBox);
            add(mySP);
        } // end of SuperPanel constructor
    } // end of: public class SuperPanel extends JPanel
    
    public class SubPanel extends JPanel{
        SuperPanel superpanel
    
        public SubPanel(SuperPanel sp) {
            superpanel = sp;
            // call superpanel methods here
        } // end of SubPanel() constructor
    } // end of SubPanel class

  3. #3
    rippon is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default

    Thanks.
    I believe I have implemented what you say correctly.
    It compiles successfully. But I get NullPointerException when I try to call the superpanel method.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    My bad, it needs to be inside the constructor.
    Java Code:
        private SubPanel mySP;
    
        public SuperPanel() {
            mySP =  new SubPanel(this); // passes a reference of the SuperPanel to the SubPanel constructor
        }

  5. #5
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by rippon View Post
    But I get NullPointerException when I try to call the superpanel method.

    strange. a tested it with this SuperClass

    Java Code:
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class SuperPanel extends JPanel {
    	public JTextField messageBox = new JTextField("public message box", 20);
        private SubPanel mySP = new SubPanel(this); // passes a reference of the SuperPanel to the SubPanel constructor
    
        public SuperPanel() {
            add(messageBox);
            add(mySP);
        } // end of SuperPanel constructor
        
        public String getMessage() {
        	return messageBox.getText();
        }
        
        public static void main(String[] args) {
    		new SuperPanel();
    	}
    } // end of: public class SuperPanel extends JPanel

    and this SubClass

    Java Code:
    import javax.swing.JPanel;
    
    public class SubPanel extends JPanel {
        SuperPanel superpanel;
    
        public SubPanel(SuperPanel sp) {
            superpanel = sp;
            // get the message from superpanel
            System.out.println(superpanel.getMessage());
        } // end of SubPanel() constructor
    } // end of SubPanel class

    and the output was: public message box

    this means, the SubPanel could get the message from the SuperClass.

  6. #6
    rippon is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default

    It works! Thanks!
    Now, why does it need to be inside the constructor?
    I think: Because, before the constructor is executed, no SuperPanel object exists, so no reference to it exists, so 'this' isn't actually pointing to anything - hence the NPE.

    Correct?

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    I thought that the problem was using "this" before the object was actually created but that is not the case. It would seem that OP has done something else incorrectly and since we cannot see the code we have no idea.

  8. #8
    rippon is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default

    What does "OP" stand for? (I presume that's a reference to me.)

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Yes, OP = original poster or original post, depending upon context.

Similar Threads

  1. Communicate with serial port
    By anilkumar_vist in forum New To Java
    Replies: 9
    Last Post: 12-24-2009, 05:53 PM
  2. Communicate JFrame with JDialog
    By BeRniTo in forum AWT / Swing
    Replies: 2
    Last Post: 08-31-2009, 02:07 PM
  3. Replies: 0
    Last Post: 04-03-2009, 01:00 PM
  4. Communicate between Windows and UNIX
    By nwboy74 in forum Networking
    Replies: 10
    Last Post: 11-04-2008, 08:53 PM
  5. Communicate vb.NET with Java
    By cachi in forum Advanced Java
    Replies: 1
    Last Post: 08-02-2007, 12:41 AM

Posting Permissions

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