Results 1 to 9 of 9
- 02-09-2011, 09:37 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 38
- Rep Power
- 0
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
- 02-09-2011, 09:58 PM #2
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
- 02-09-2011, 10:49 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 38
- Rep Power
- 0
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.
- 02-09-2011, 10:54 PM #4
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 }
- 02-09-2011, 11:08 PM #5
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.
- 02-09-2011, 11:11 PM #6
Member
- Join Date
- Nov 2010
- Posts
- 38
- Rep Power
- 0
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?
- 02-09-2011, 11:13 PM #7
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.
- 02-09-2011, 11:18 PM #8
Member
- Join Date
- Nov 2010
- Posts
- 38
- Rep Power
- 0
What does "OP" stand for? (I presume that's a reference to me.)
- 02-09-2011, 11:20 PM #9
Similar Threads
-
Communicate with serial port
By anilkumar_vist in forum New To JavaReplies: 9Last Post: 12-24-2009, 05:53 PM -
Communicate JFrame with JDialog
By BeRniTo in forum AWT / SwingReplies: 2Last Post: 08-31-2009, 02:07 PM -
How to communicate with Visual Editor for Eclipse 3.2
By karim in forum EclipseReplies: 0Last Post: 04-03-2009, 01:00 PM -
Communicate between Windows and UNIX
By nwboy74 in forum NetworkingReplies: 10Last Post: 11-04-2008, 08:53 PM -
Communicate vb.NET with Java
By cachi in forum Advanced JavaReplies: 1Last Post: 08-02-2007, 12:41 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks