Matthew Vedua Matthew Vedua - 6 months ago 16
Java Question

How to wait for a value to be reached to continue the method?

btnStraight = new JButton("Straight");
btnStraight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {

/* Delay if statement until an ArrayList reaches a certain size */

if(RouletteHelperMethods.doBet("Straight") == 1){
textArea.setText("Congrats, you won!");
} else {
textArea.setText("Sorry, you lost.");
}
}
});


The user is supposed to click on a button, then is told to click on JTextFields to add them to an ArrayList. How do I delay the if statement until this happens?

Answer

You need to re-think how your program runs since you're coding this as if you have written a linear console program, but that's not the case. Instead you're coding an event-driven GUI program, and so instead of having code "wait" you should be reacting to events, and to change in state.

If you need further details on this, please flesh out your question, including giving more about just what your program is supposed to be doing, what ArrayList you mean, and including providing here (and not in a link) a valid minimal example program (please read the link).

I was trying to play with your logical needs, and came up with a small program that creates a bunch of JTextFields. When clicked the text they hold is added to a JTextArea and the fields are added to a selected fields ArrayList. When the number of fields added to the list is 4, then the logic runs. This is called from a FocusListener that is added to all the text fields. For example:

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;

@SuppressWarnings("serial")
public class EventDrivenGui extends JPanel {
    private static final int FIELD_SIZE = 10;
    private static final int SELECTED_FIELD_SIZE = 4;
    protected static final Color SELECTED_COLOR = Color.PINK;
    private JButton btnStraight;
    private JTextArea textArea = new JTextArea(20, 40);
    private List<JTextField> selectedFieldList = new ArrayList<>();
    private List<JTextField> fieldList = new ArrayList<>();

    public EventDrivenGui() {
        btnStraight = new JButton("Straight");
        btnStraight.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (selectedFieldList.size() > 0) {
                    return;
                }
                for (JTextField textField : fieldList) {
                    textField.setEnabled(true);
                }
                JOptionPane.showMessageDialog(btnStraight, "Please Select a text field",
                        "Make Selection", JOptionPane.INFORMATION_MESSAGE);

            }
        });

        textArea.setFocusable(false);

        JPanel fieldPanel = new JPanel(new GridLayout(0, 1, 5, 5));
        for (int i = 0; i < FIELD_SIZE; i++) {
            JTextField field = new JTextField("Field " + (i + 1), 10);
            field.setEnabled(false); // so they can't select it prematurely
            fieldList.add(field);
            fieldPanel.add(field);

            field.addFocusListener(new FocusAdapter() {
                @Override
                public void focusGained(FocusEvent e) {
                    JTextField tf = (JTextField) e.getComponent();
                    if (!selectedFieldList.contains(tf)) {

                        textArea.append(tf.getText() + "\n");
                        tf.setBackground(SELECTED_COLOR);
                        selectedFieldList.add(tf);
                        if (selectedFieldList.size() >= SELECTED_FIELD_SIZE) {
                            if (RouletteHelperMethods.doBet("Straight") == 1) {
                                textArea.append("Congrats, you won!\n");
                            } else {
                                textArea.append("Sorry, you lost.\n");
                            }

                            reset();
                        }
                    }

                }
            });

        }

        add(btnStraight);
        add(new JScrollPane(textArea));
        add(fieldPanel);
    }

    protected void reset() {
        selectedFieldList.clear();
        for (JTextField jTextField : fieldList) {
            jTextField.setEnabled(false);
            jTextField.setBackground(null);
        }
    }

    private static void createAndShowGui() {
        EventDrivenGui mainPanel = new EventDrivenGui();

        JFrame frame = new JFrame("EventDrivenGui");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

class RouletteHelperMethods {

    public static int doBet(String bet) {
        return Math.random() > 0.5 ? 1 : 0;
    }

}