0

I'm trying to call a Getter from another class, and when I do, it returns and prints 0 every time. It generates a random number when you click the easyButton. After I click that button, it still shows zero after I call the getter with the enterInputButton. Here is the code. I have print statements to show that it doesn't work. I do this in other classes, and it works in my other small projects. I just dont understand why it returns 0 after I call it in the print statement, when I click the enterInputButton. Thank you so much! (Its my first time posting here)

public class UserInputPanel extends JPanel
{
    private DifficultyPanel grabDiff;
    private SpringLayout baseLayout;

    private JTextField userGuessField;
    private JButton enterInputButton;

    private int userGuess;

    //Class that needs the random Number
    public UserInputPanel() 
    {
        grabDiff = new DifficultyPanel();

        baseLayout = new SpringLayout();

        userGuessField = new JTextField(2);
        enterInputButton = new JButton("Enter Guess");

        buildPanel();
        buildWindow();
        buildListeners();
    }

    private void buildPanel()
    {
        setBackground(Color.RED);
        setPreferredSize(new Dimension(200,200));
        setLayout(baseLayout);
        add(userGuessField);
        add(enterInputButton);
    }

    private void buildWindow()
    {

    }

    private void buildListeners()
    {
        enterInputButton.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent clicked)
                {
                    parseInput();
                    System.out.println("MY INPUT " + userGuess);

                    //This line prints it to 0
                    System.out.println("GEN NUM : " + grabDiff.getSelectedNumber());
                }
            });
    }

    private void parseInput()
    {
        userGuess = Integer.parseInt(userGuessField.getText());
    }
}

And here is the class that has the variable.

public class DifficultyPanel extends JPanel
{
    private SpringLayout baseLayout;

    private JButton easyButton;

    private int selectedNumber;

    public DifficultyPanel()
    {
        baseLayout = new SpringLayout();

        easyButton = new JButton("Easy");

        buildPanel();
        buildListeners();
    }

    private void buildPanel()
    {
        setBackground(Color.BLUE);
        setPreferredSize(new Dimension(200,200));

        setLayout(baseLayout);
        add(easyButton);
    }

    private void buildListeners()
    {
        easyButton.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent click)
                {
                    selectedNumber = (int) (Math.random() * 51);
                    System.out.println(selectedNumber);
                }
            });
    }


    public int getSelectedNumber()
    {
        System.out.println(selectedNumber);
        return this.selectedNumber;
    }
}
Whatty
  • 79
  • 2
  • 5
  • Since "it" works in other projects - what did you do differently? When is the number changed, i.e. the `easyButton` being clicked? – Dominik Sandjaja Feb 09 '16 at 20:48
  • I dont see you have set it anywhere. You made grabDiff as a new object and just call that get select number – logger Feb 09 '16 at 20:48
  • 1
    `selectedNumber` is being set in the `ActionListener` on `easyButton`. As long as the methods are called and the button is pressed in the correct order, the `selectedNumber` field ought to be set to something. – khelwood Feb 09 '16 at 20:51
  • Sorry! Added the bold part at the top. Might clear up some confusion. – Whatty Feb 09 '16 at 20:53

1 Answers1

0

You are creating an instance of DifficultyPanel called grabDiff in UserInputPanel as a private field, and trying to get the number from that. But you never add that grabDiff to the UserInputPanel or any other container, so the user can never click the button in it. I suspect you have another instance of DifficultyPanel somewhere else (in some code you didn't post), and that is the one that has the number set in it.

[Edit]

If you wanted to add grabDiff to your DifficultyPanel, you could put an add line into DifficultyPanel.buildPanel, something like this:

private void buildPanel()
{
    setBackground(Color.RED);
    setPreferredSize(new Dimension(200,200));
    setLayout(baseLayout);
    add(grabDiff); // <- new line
    add(userGuessField);
    add(enterInputButton);
}
khelwood
  • 55,782
  • 14
  • 81
  • 108
  • How do you add grabDiff to the UserInputPanel then? Here is my Github with the classes. https://github.com/Wh3at1y/GuessingGAMEAGAIN/tree/master/src/game – Whatty Feb 09 '16 at 21:17