1

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();
            }

        }

    }
}
Community
  • 1
  • 1
  • 1
    In your `calculateMean()` you are setting `calculate.setEnabled(false);` - where are you re-enabling this ? – Bhaskar May 03 '14 at 20:20
  • Bhaskar is right you disabled your button calculate. To Enable it calculate.setEnabled(true); – phil652 May 03 '14 at 20:42
  • @Bhaskar I thought the setEnabled method would change whether the button was pressed or not so I put it to false, thinking that that meant it would not be pressed. Is this not correct? Am I getting it the wrong way round? –  May 04 '14 at 08:02
  • `setEnabled(false)` will disable the button ( in a pressed mode in your words ) - please read the documentation and try different ways. Thats the way to learn things. – Bhaskar May 04 '14 at 09:22

1 Answers1

0
  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.
camickr
  • 321,443
  • 19
  • 166
  • 288
  • Sorry, I did what you said but now the button goes white when the dialog box closes. Originally it was blueish but still inactive and now it is white and inactive. You know anything else that might be wrong? Thank you for your answer though :) –  May 04 '14 at 08:06
  • Never mind. I think you did help me but I was calling the setEnabled(false) when I should have said setEnabled(true) and now it's working! –  May 04 '14 at 14:00