LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-06-2004, 01:03 PM   #1
nro
Member
 
Registered: Oct 2003
Distribution: Mandrake 9
Posts: 46

Rep: Reputation: 15
Java window not opening when loop included


My problem: I have a program that calculates the value of pi using a loop.
When I take the loop out, the java window will pop up but when I include the loop, nothing pops up, although I do get the console output.

Everything compiles correctly but I have a feeling I'm possibly overloading my computer with the loop(not an old computer, but not a new one either).

Code:
package PiCounter;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class PiGUI extends JFrame {
    
    JTextField piDisplay   = new JTextField();
    JTextField calcDisplay = new JTextField();
    JLabel calcLabel       = new JLabel("# of calculations");
    JLabel piLabel         = new JLabel("Value of PI");
    
    Calculator calc = new Calculator(this);
    
    PiGUI() {
        // Frame listener - closeing window
        addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}});
        
        // Configure GUI layout
        Container inter = getContentPane();
        inter.setLayout ( null );
        
        // Set the box not editable
        piDisplay.setEditable(false);
        calcDisplay.setEditable(false);
        
        // Set the box background
        piDisplay.setBackground(Color.black);
        calcDisplay.setBackground(Color.black);
        
        // Set the box forground
        piDisplay.setForeground(Color.red);
        calcDisplay.setForeground(Color.red);
        
        // Add components to GUI
        inter.add(piDisplay);
        inter.add(calcDisplay);
        inter.add(piLabel);
        inter.add(calcLabel);
        
        piLabel.setBounds(5,5,150,20);
        piDisplay.setBounds(5,30,150,40);
        calcLabel.setBounds(5,75,150,20);
        calcDisplay.setBounds(5,100,150,40);
        
        setSize(170,170); // Size of window
        setTitle("PI Calculator"); // Title of window
        setVisible(true); // Set visible
        setResizable(false); // Disallow resize
    }
}

class Calculator {
    PiGUI actionGUI;
    
    int calcCounter   = 0;
    double denomCount = 3;
    double pi         = 4;
    
    public Calculator(PiGUI tempGUI) {
        actionGUI = tempGUI;
        start();
    }
    
    public void start() {
        while( true ) {
            calcSub();
            calcAdd();
            if((calcCounter % 10000000) == 0) {
                    actionGUI.piDisplay.setText(String.valueOf(pi));
                    actionGUI.calcDisplay.setText(String.valueOf(calcCounter));
                    System.out.println(calcCounter + ": " + pi);
            }
        }
    }
    
    public void calcSub() {
        pi -= (4/denomCount);
        denomCount += 2;
        calcCounter++;
    }
    
    public void calcAdd() {
        pi += (4/denomCount);
        denomCount += 2;
        calcCounter++;
    }
}

public class PiCounter {
    
    public static void main(String[] args) {
        PiGUI piGUI = new PiGUI();
    }
    
}
 
Old 09-07-2004, 11:30 AM   #2
djgerbavore
Member
 
Registered: Jun 2004
Location: PA
Distribution: Fedora (latest git kernel)
Posts: 458

Rep: Reputation: 30
[code]
setSize(170,170); // Size of window
setTitle("PI Calculator"); // Title of window
setVisible(true); // Set visible
setResizable(false); // Disallow resize
[\code]

after the end of this block of code try adding
this.show();


-Gerb
 
Old 09-07-2004, 02:16 PM   #3
aaa
LQ Guru
 
Registered: Jul 2003
Location: VA
Distribution: Slack 10.1
Posts: 2,194

Rep: Reputation: 47
Two problems here:
Code:
    Calculator calc = new Calculator(this);
    
    PiGUI() {
        // Frame listener - closeing window
        addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}});
The constructor of Calculator calls start(), which is an infinite loop. The Calculator constructor is called when the PiGUI is created, which causes the program to loop before the gui is even created. Replace "Calculator calc = new Calculator(this);" with just "Calculator calc;". Then do "calc = new Calculator(this);" in your constructor after you show the window.
The second problem is very minor, instead of "addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}});", use "setDefaultClosingOperation(JFrame.EXIT_ON_CLOSE);".

Also, while fixing your problem, beware of the Swing with threads problem. Whenever you use Swing/AWT, new threads are created. Modifying Swing gui objects outside of the gui event thread (like in the main thread) will cause hard to detect problems. Any calling of setText() after the window is already visible needs to be done in the event thread, like this:
Code:
SwingUtilities.invokeLater(new Runnable(){
public void run(){
  //insert gui code like setText() here
}
});
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
why mandriva 10.2 gnome2.8 system freezes upon resizing/opening window/new window skaramanger Mandriva 8 08-30-2005 10:22 AM
why mandriva 10.2 gnome2.8 system freezes upon resizing/opening window/new window skaramanger Mandriva 2 08-15-2005 04:38 PM
Implementing a blocking loop (Java or C) Miaire Programming 4 04-28-2005 02:22 PM
Java Stopping an Infinite loop oulevon Programming 3 10-18-2004 10:11 PM
window opening problem!!!! venkat Fedora 0 08-19-2004 01:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration