Question

Ok, I am sorry I am repeating questions that have already been asked but I have searched and searched and searched and nobody's answers seemed to have helped me... I tried the following questions:

JButton "stay pressed" after click in Java Applet

JButton stays pressed when focus stolen by JOptionPane

(I apologize if I'm just being dumb.. it is hard to relate to my code)

I have tried everything: using another thread to handle all the stuff, changing the JFrame to a JDialog as apparently they are "modal" so it would work independently. But that didn't seem to work either. I am stuck now so I am using my last resource (asking Stack Overflow).

What I am trying to do is get the user to enter some numbers in a textfield (4,2,7) then they press a JButton "Calculate Mean" and it finds the mean of the numbers and displays it in a JOptionPane message. When the user closes the JOptionPane dialog box they should be able to edit the numbers and do it again but the "Calculate Mean" button stays pressed and the user can't do anything but close the window. Even pressing the Tab key doesn't change anything. Does anyone know why this is? My code is down below:

PLEASE FORGIVE ME IF MY CODE IS HARD TO READ! I spent a very long time trying to indent it all correctly and I also tried to make it as short as possible by taking out any bits unrelated to the question. I was unsure which bits to take out so there still might be some unnecessary bits... I am sorry for my messy code but this is the code:

package MathsProgram_II;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;

public class Mean implements Runnable {
    JFrame meanFrame = new JFrame(); //I tried changing this to dialog
    JPanel meanPanel = new JPanel(new GridBagLayout());
    JLabel enterNums = new JLabel("Enter Numbers: ");
    JTextField txtNums = new JTextField(20);
    JButton calculate = new JButton("Calculate Mean");

    boolean valid = true;
    double answer = 0;
    ButtonListener bl = new ButtonListener();


    public synchronized double[] getArray() {
        String nums = txtNums.getText();
        String[] numsArray = nums.split(",");
        double[] doubleArray = new double[numsArray.length];
        if (nums.isEmpty() == true) {
            JOptionPane.showMessageDialog(meanFrame, "You did not enter     anything!",
                    "Fail", JOptionPane.ERROR_MESSAGE);

            valid = false;
            calculate.setEnabled(false);
        } else {
            for (int i = 0; i < numsArray.length; i++) {
                try {
                    doubleArray[i] = Double.parseDouble(numsArray[i]);
                } catch (NumberFormatException nfe) {
                    JOptionPane.showMessageDialog(meanFrame, "Error getting numbers!",
                            "Error", JOptionPane.ERROR_MESSAGE);
                    valid = false;
                }
            }
        }
        return doubleArray;
    }


    public synchronized void calculateMean() {
        ArrayList<Double> numbersList = new ArrayList<Double>(20);
        double[] theNumbers = getArray();
        double tempAnswer = 0;
        if (valid == true) {
            int length = theNumbers.length;
            for (int i = 0; i < theNumbers.length; i++) {
                numbersList.add(theNumbers[i]);
            }
            for (int i = 0; i < length; i++) {
                double y = numbersList.get(i);
                tempAnswer = tempAnswer + y;
            }
            this.answer = tempAnswer / length;
            //I ALSO TRIED DOING THIS:
            txtNums.requestFocus();
            calculate.setEnabled(false);

            showMean();
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
            }

        }

    }

    public void showMean() {
        JOptionPane.showMessageDialog(meanFrame, "The Mean: " + answer, "The Mean of      Your Numbers", JOptionPane.INFORMATION_MESSAGE);
    }

    private class ButtonListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == calculate) {
                meanFrame.remove(meanPanel);
                meanFrame.setVisible(true);
                calculateMean();
            }

        }

    }
}
Was it helpful?

Solution

  1. Don't remove the panel from your mainframe.
  2. Don't use "synchronized" on your "calculateMean()" method. The code is executed on the Event Dispatch Thread (EDT) so it will be single threaded.
  3. Don't use a Thread.sleep() on the EDT. This will prevent the GUI from repainting itself.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top