• Post Reply Bookmark Topic Watch Topic
  • New Topic

Endless loop/call while Calling a dialog box via separate/another JFrame class in NetBeans IDE.  RSS feed

 
Khushi Sing
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi,
- Ive used Netbeans IDE to create a simple Swing JFrame container, class NewJFrame.
- NewJFrame calls another JFrame with dialog in it. Problem is there is an endless call made to it. Here's the issue relevant piece of code:
//Code for calling class
package p;
public class NewJFrame extends javax.swing.JFrame implements ActionListener
{
.........
public NewJFrame()
{
initComponents(); //IDE auto generated code for binding
jButton1.addActionListener(this);
}

public void actionPerformed(ActionEvent e)
{

// System.out.println("from actionPerformed");
String s1 = new String();
s1 = jTextArea1.getText();
int t1 = s1.length();
if( t1 > 70)
{
NewJFrame2 obj2 = new NewJFrame2();
}
PSVM(String[] args)
{
//calling constructor of NewJFrame() , not relevant here.
}

****************************
//called class

package p;
public class NewJFrame2 extends javax.swing.JFrame implements Runnable
{
public NewJFrame2()
{
initComponents(); //IDE auto generated code for binding
Thread threadX = new Thread(this);
//System.out.println("Inside COnstructor");
}
PSVM(String[] args)
{
//nothing relevant here
}

public void run()
{
JFrame f = new NewJFrame2(); //Frame container to hold dialog window
f.setVisible(true); //setting it visible
JPanel message = new JPanel(); //Creating a JPanel
message.add(new JLabel("This is a dialog ")); // Adding a label to the JPanel
JDialog dialog = new JDialog(f, "Dialog"); // Binding dialog in frame
dialog.setContentPane(message);
dialog.pack();
dialog.setLocationRelativeTo(f);
dialog.setVisible(true);
}
} // Class ends

I searched but i cannot understand why run class is getting called multiple times.
I have attached the screen prints for both the screens. 2nd screen print(of called class) is very shallow since screen was blinking unstopped due to multiple calls, but i have attached a faint image of it.

Thankyou much.



blinkingJFrame2.jpg
[Thumbnail for blinkingJFrame2.jpg]
NewJFrame-class1
 
Piet Souris
Rancher
Posts: 1947
66
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Fraggy,

are you calling threadX.run() or threadX.start() somewhere in your NewJFrame2-code? In that run method,
you create another instance of NewJFrame2. Since you are ending up in an infinite loop, this would be
my first suspicion.

Greetz,
Piet
 
Khushi Sing
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Piet,
Thanks for replying.
- No, i havent used any thread methods inside.
- To simplify the code, i just removed implementation of class Runnable, because:
- Calling class calls the called dialog class using an object (obviously), and im just using this.run1() to call the function for new JFrame and Dialog display.. and the code is:

//calling class

public class NewJFrame extends javax.swing.JFrame implements ActionListener
{
public NewJFrame()
{
initComponents(); //IDE auto binding
jButton1.addActionListener(this);
}

public void actionPerformed(ActionEvent e)
{

System.out.println("ok");
String s1 = new String();
s1 = jTextArea1.getText();
int t1 = s1.length();
if( t1 > 2)
{
System.out.println("aa");
NewJFrame2 obj2 = new NewJFrame2(); /* Calling NewJFrame2 class here*/
} //if ends
}//over-ridden actionPerformed method ends

*****************************
Called Class - Modifications made here:
//Not Implementing Runnable
public class NewJFrame2 extends javax.swing.JFrame
{
public NewJFrame2()
{
initComponents();
this.run1();

}
private void run1()
{
//System.out.println("Inside run");
JFrame f = new NewJFrame2();
f.setVisible(true);
JPanel message = new JPanel();
message.add(new JLabel("This is a dialog "));
JDialog dialog = new JDialog(f, "Dialog");
dialog.setContentPane(message);
dialog.pack();
dialog.setLocationRelativeTo(f);
dialog.setVisible(true);
} //run1 method ends
} //called class, NewJFrame2 ends

*******************
Its a single thread, im just not able to figure out why its going in an endless call.. This is all to the code really.

Thanks.




 
Piet Souris
Rancher
Posts: 1947
66
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I see. Well, if you look at the code:


you see that in the actionPerformed() method, you create an instance of NewJFrame2,
and if you look at the constructor of that class, you first call 'initComponents", then call
'this.run1'. Now, if you look at the 'run1' method, it creates a new instance of NewJFrame2,
so in that constructor, 'this.run1' is called, which creates another NewJFrame2, which ...

You see the infinite loop arising here?

Greetz,
Piet
 
Khushi Sing
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I totally got ya Piet!
Thanks.

Here's the modified code for the called class (along with auto generated code, i dragged JDialog from palette before using it, JDialog1, and then made necessary changes:




Thanks a ton and have a great weekend!
 
Piet Souris
Rancher
Posts: 1947
66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome, and have a great weekend too!

Just a tip for other questions you might have:

If you list some code, you probably do that by copying the
code into the answering panel. Now, having done that, select that code
by dragging the mouse over it, and then click on the 'code' button at the
top of the answering panel. If you then look at your reply (use the 'preview'
button), you see that your code is neatly laid out. I did this in my first reply,
and you see that it makes the code very much easier to read.

Greetz,
Piet

Edit: forgot completely: welcome to the ranch!
 
Khushi Sing
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Really appreciate it, neat and xml next time.

Thanks!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!