dreadrocksean -6 Newbie Poster

Hi. I am having trouble understanding why, if I use setEnabled() inside a button listener, my end result where I update the text inside a text box doesnt work the same way as when I comment out the setEnabled() lines.

I am making a simple pi generator based on a particular algorithm.
I am using a for loop where pi gets more accurate with every loop.
At the end of every loop I send the answer to be set in the textBox.

The calculation itself is a method called pi() inside of a class called Equations (I eventually want to include other irrational constants).
The window is a separate class extending Frame and it holds 2 buttons, "start" and "stop", a textBox and a label.
The Listener for the "start" button calls the method start() in its own class. start(), in turn, spins off a separate thread that calls new Equations(...).pi() in that new thread's run() method.
But before the listener calls start(), it enables the two buttons accordingly.

This works if I comment out the setEnabled(...) lines.
If I use invokeLater(...) in the pi() method for the setText(...) lines in order to put them on the EDT, the text box updates very slowly. Like once every second or so.

public class Equations {
    private final EquationWindow equationWindow;
    private double pi;
    private int count;

    public Equations(EquationWindow equationWindow) {
        this.equationWindow = equationWindow;
    }

    public void pi() {
        double divisor = 1.0;
        double numerator = 1.0;
        double curr = 0;
        for (int i=1; EquationWindow.equationThread == Thread.currentThread(); i++) {
            count = i;
            curr += numerator/(divisor*divisor);
            divisor += 1;
            numerator *= -1;
            pi = Math.sqrt(12*curr);
            //equationWindow.setResult("pi: ", pi);
            //equationWindow.setCount(count);
            ///*

             SwingUtilities.invokeLater(new Runnable() {
                public void run() {
            System.out.println("pi: " + pi);
                    equationWindow.setResult("pi: ", pi);
                    equationWindow.setCount(count);
                }
            });
            //*/
        }
    }
}


public class EquationWindow extends JFrame {
    public static volatile Thread equationThread;
    private JComponent rootPanel;
    private JLabel resultLabel = new JLabel("Result");
    private JTextField resultField= new JTextField("no value yet");
    private JLabel count = new JLabel("0");

    public EquationWindow() {
        showWindow();
        repaint();
    }

    public void setResult(String type, double d) {
        resultField.setText("" + d);
    }

    public void setCount(int i) {
        count.setText("" + i);
    }

    public final void showWindow() {
        if (rootPanel != null) remove(rootPanel);
        setTitle ("Recursive Equation");
        setDefaultLookAndFeelDecorated(true);
        setBackground(Color.gray);
        setPreferredSize(new Dimension(300,100));
        setLayout(new BorderLayout());
        rootPanel = new JPanel(new BorderLayout());
        rootPanel.setOpaque(true);
        setContentPane(rootPanel);
        final JPanel controls = new JPanel();
        final JPanel main = new JPanel();
        final JButton start = new JButton("Start");
        final JButton stop = new JButton("Stop");
        start.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent arg0) {
                        start.setEnabled(false);
                        stop.setEnabled(true);
                        startPi();
                    }
                });
        stop.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent arg0) {
                        stop.setEnabled(false);
                        start.setEnabled(true);
                        stop();
                    }
                });
        stop.setEnabled(false);
        controls.add(start);
        controls.add(stop);
        resultField.setPreferredSize(new Dimension(175, 20));
        main.add(resultLabel);
        main.add(resultField);
        main.add(count);
        rootPanel.add(main, BorderLayout.CENTER);
        rootPanel.add(controls, BorderLayout.NORTH);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    public void startPi() {
        Thread t = new Thread(new Runnable() {
            public void run() {
                new Equations(EquationWindow.this).pi();
            }
        }, "equationThread");
        equationThread = t;
        t.start();
    }

    public void stop() {
        equationThread = null;
    }

    public void paintComponents(Graphics g) {
        super.paintComponents(g);
        System.out.println("in paintComponents()");
    }

    public void paint(Graphics g) {
        super.paint(g);
        System.out.println("in paint()");
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.