1

I have been coding for a card game and cannot get my method to wait for a button to be pressed.

The general code goes like this. When I run the code, doTask() has a segment where it needs to wait for a button to be pressed. However, the method does not wait for the button and just loops through.

I am currently thinking to have a while loop with a boolean (buttonIsPressed) which will be triggered true in actionPerformed(e). However, is there a simpler way to do this?

Any help would be appreciated. Thank you.

public class Test {

    public Test()
    {   
        // all vars instantiated

        while (!(taskLeft==0))
        {
            doTask();
            taskLeft--;
        }

    }

    private class Handler implements ActionListener
    {
        public void actionPerformed(ActionEvent arg0) {

            // update information in doTask()

        }

    }

}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
user2999870
  • 345
  • 4
  • 12
  • @HovercraftFullOfEels yeah, just noticed that. Guess I should've thought about the question a bit before closing it. I'm just getting a bit tired of this kind of question –  Feb 03 '16 at 02:04

1 Answers1

3

Yours is a classic XY Problem where you ask how to solve a specific code problem when the best solution is to use a completely different approach. You're thinking of how do I code this event-driven GUI program to work as a linear console program, and that's not how you should approach this. Instead look at changing object state and basing response of the object to events based on its state.

So get rid of the while loop, and instead do your task when the button is pushed based on the state of the GUI. The details of any solution will depend on the details of your problem, something you may wish to share with us.

So for instance, here taskLeft represents a "state" of the TaskEx object, and your Handler's response will depend on the state of this variable.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class TaskEx extends JPanel {
    private int taskLeft = 10;

    public void doTask() {
        // 
    }

    private class Handler implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (taskLeft > 0) {
                doTask();
                taskLeft--;
            }
        }
    }
}

An actually functioning example:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;

@SuppressWarnings("serial")
public class TaskEx extends JPanel {
    private int taskLeft = 10;
    private JLabel taskCountLabel = new JLabel(String.valueOf(taskLeft));

    public TaskEx() {
        JPanel northPanel = new JPanel();
        northPanel.add(new JLabel("Tasks Left:"));
        northPanel.add(taskCountLabel);

        JPanel centerPanel = new JPanel();
        centerPanel.add(new JButton(new Handler("Push Me")));

        setLayout(new BorderLayout());
        add(northPanel, BorderLayout.PAGE_START);
        add(centerPanel, BorderLayout.CENTER);
    }

    public void doTask() {
        taskCountLabel.setText(String.valueOf(taskLeft));
    }

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

        JFrame frame = new JFrame("Task Example");
        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();
        });
    }

    private class Handler extends AbstractAction {
        public Handler(String name) {
            super(name);
            putValue(MNEMONIC_KEY, (int) name.charAt(0));
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            if (taskLeft > 0) {
                taskLeft--;
                doTask();
            }
        }
    }

}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373