Results 1 to 4 of 4
- 06-11-2011, 03:35 AM #1
Senior Member
- Join Date
- Jan 2011
- Location
- Rizal Province, Philippiines
- Posts
- 167
- Rep Power
- 0
Simple linking of class1 to class2 Problem help me please
I want to link my class1 to class2 but I have an error
This is supposed to be a simple chat program, when I hit the send button in class1, the class2 will appear.java.lang.StackOverflowError
at sun.awt.Win32GraphicsConfig.getBounds(Native Method)
at sun.awt.Win32GraphicsConfig.getBounds(Unknown Source)
at java.awt.Window.init(Unknown Source)
at java.awt.Window.<init>(Unknown Source)
at java.awt.Frame.<init>(Unknown Source)
at java.awt.Frame.<init>(Unknown Source)
at javax.swing.JFrame.<init>(Unknown Source)
Here's the code:
Class1.java
class2.javaJava Code:public class class1 extends JFrame { class2 c2 = new class2(); public class1() { lblHeader = new JLabel(); jScrollPane1 = new JScrollPane(); txtAreaChat = new JTextArea(); txtEnterChat = new JTextField(); btnSend = new JButton(); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); getContentPane().setLayout(null); lblHeader.setText("Chatter 1"); txtAreaChat.setColumns(20); txtAreaChat.setEditable(false); txtAreaChat.setRows(5); jScrollPane1.setViewportView(txtAreaChat); btnSend.setText("Send"); btnSend.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ c2.setVisible(true); } } ); getContentPane().add(lblHeader); getContentPane().add(jScrollPane1); getContentPane().add(txtEnterChat); getContentPane().add(btnSend); lblHeader.setBounds(70, 10, 50, 14); txtEnterChat.setBounds(10, 220, 160, 20); jScrollPane1.setBounds(10, 40, 166, 170); btnSend.setBounds(60, 250, 75, 23); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); setBounds((screenSize.width-196)/2, (screenSize.height-312)/2, 196, 312); }
Please help me guys..Java Code:public class class2 extends JFrame { class1 c1 = new class1(); public class2() { lblHeader = new JLabel(); jScrollPane1 = new JScrollPane(); txtAreaChat = new JTextArea(); txtEnterChat = new JTextField(); btnSend = new JButton(); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); getContentPane().setLayout(null); lblHeader.setText("Chatter 1"); txtAreaChat.setColumns(20); txtAreaChat.setEditable(false); txtAreaChat.setRows(5); jScrollPane1.setViewportView(txtAreaChat); btnSend.setText("Send"); btnSend.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ c1.setVisible(true); } } ); getContentPane().add(lblHeader); getContentPane().add(jScrollPane1); getContentPane().add(txtEnterChat); getContentPane().add(btnSend); lblHeader.setBounds(70, 10, 50, 14); txtEnterChat.setBounds(10, 220, 160, 20); jScrollPane1.setBounds(10, 40, 166, 170); btnSend.setBounds(60, 250, 75, 23); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); setBounds((screenSize.width-196)/2, (screenSize.height-312)/2, 196, 312); }
-
You've got infinite recursion going on. If you create a class1 object, it creates a class2 object which creates a class1 object which creates a class2 object, which creates a class1 object which creates a class2 object, which creates a class1 object which creates a class2 object, which creates a class1 object which creates a class2 object, which creates a class1 object which creates a class2 object, which...
Solution: don't do this.
- 06-11-2011, 03:44 AM #3
Senior Member
- Join Date
- Jan 2011
- Location
- Rizal Province, Philippiines
- Posts
- 167
- Rep Power
- 0
-
Better for you to fix your own code. But to solve it, only have one class create an instance of another. Don't have both classes create instances of the other.
For instance, what you have is this:
If you run this, you'll see the same error as in your code and for the very same reason.Java Code:public class ClassRecursion { public static void main(String[] args) { Class1 c1 = new Class1(); } } class Class1 { Class2 c2 = new Class2(); public Class1() { System.out.println("inside class 1"); } } class Class2 { Class1 c1 = new Class1(); public Class2() { System.out.println("inside class 2"); } }
What you want instead is to have one of the classes create an instance of the other, and the other class to receive a reference to the first, something like so:
Java Code:public class ClassRecursion { public static void main(String[] args) { Class1 c1 = new Class1(); } } class Class1 { Class2B c2b = new Class2B(this); // pass a reference to the current Class1 into Class2B constructor public Class1() { System.out.println("inside class 1"); } } class Class2B { Class1 c1; public Class2B(Class1 c1) { System.out.println("inside class 2B"); this.c1 = c1; } }
Similar Threads
-
[SWING]Adding JButton(class1) to JPanel(class2)
By equal in forum New To JavaReplies: 8Last Post: 02-20-2011, 01:09 AM -
problem of linking text file and java program
By binweifong in forum New To JavaReplies: 9Last Post: 12-08-2010, 04:06 PM -
simple line problem / for loop problem
By helpisontheway in forum New To JavaReplies: 1Last Post: 11-17-2009, 06:12 AM -
Linking of exe files
By archu2friends in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 02-06-2008, 06:08 AM -
<URGENT> problem after linking .jnlp
By bongia in forum New To JavaReplies: 14Last Post: 11-18-2007, 05:57 PM


LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks