0

consider the following code.

import edu.cmu.ri.createlab.terk.robot.finch.Finch;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class RobotControl extends JFrame {
   public static void main (String args[])  {

    RobotControl GUI = new RobotControl();
    GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    GUI.setSize(500,500);
    GUI.setVisible(true);
    GUI.setTitle("RobotControl");
}

//The following are declarations of object variables.
    private Finch myf;
    private JButton front;
    private JButton back;
    private JButton left;


public RobotControl() { 
    myf = new Finch();
    setLayout (new FlowLayout());


    front = new JButton("front");
    add(front);
    front.addActionListener(new FrontButtonListener(myf));
    back = new JButton("back");
    add(back);
    back.addActionListener(new BackButtonListener(myf));
    left = new JButton("left");
    add(left);
    left.addActionListener(new LeftButtonListener(myf));

    }
public class FrontButtonListener implements ActionListener {
    public FrontButtonListener(Finch myf) {
        // TODO Auto-generated constructor stub
    }
                public void actionPerformed(ActionEvent arg0) {
                    myf.setWheelVelocities(100,100,10000);
                }
    }
public class BackButtonListener implements ActionListener{
    public BackButtonListener(Finch myf){   
    }
                public void actionPerformed(ActionEvent arg0) {
                    myf.setWheelVelocities(-100,-100,10000);
                }
    }   
public class LeftButtonListener implements ActionListener{
    public LeftButtonListener(Finch myf){
    }
                public void actionPerformed(ActionEvent arg0){
                    myf.setWheelVelocities(0, 200, 1000);
                }   

Now, the code above will create a GUI, with three buttons, front, back, and left. I need some advice on how to make the program wait for all three buttons to be clicked before it runs, as opposed to clicking one button at a time.

Hustl3r28
  • 211
  • 7
  • 16

3 Answers3

1

Create a boolean variable for each button and enable them to true when their correspoding button is clicked.

private  boolean  firstClicked = false;
private  boolean  secondClicked = false;
private  boolean  thirdClicked = false;
......
......
//set these boolean values in their onClick actionPerfomed method
if(firstClicked && secondClicked && thirdClicked){
//do whatever operations you want after three buttons have been clicked
}

Note: You need to have these boolean variables in Class level

Keerthivasan
  • 12,760
  • 2
  • 32
  • 53
1

Use JToggleButton or JCheckBox to retain each button's state. Assuming a List<JToggleButton> named list, you could calculate an allTrue predicate as follows:

boolean allTrue = true;
for (JToggleButton b : list) {
    allTrue &= b.getSelected;
}

Enable the desired functionality only when allTrue is true. A related example is seen here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

Keep track of the number of them that have been clicked already (with an int). To do this, increment the int only when a button is clicked that hasn't already been clicked (you can keep track of this with a boolean). When the int equals 3, regardless of the order in which the buttons were clicked, call an outside method that does what is currently in the 3 actionPerformed methods. Something like this for the left button, and similarly for the other ones:

    public void actionPerformed(ActionEvent arg0) {
        if(!leftClicked) {
            leftClicked = true;
            numButtonsClicked++;
        }
        countLeftButtonClicks++;
        if(numButtonsClicked == 3) {
            newMethodThatWritesToLogFileToo();
        }
    }
Martin Dinov
  • 8,757
  • 3
  • 29
  • 41
  • What if i want a button clicked more tan once? – Hustl3r28 Jan 18 '14 at 11:25
  • Do you want to keep track of how many times each of the buttons was clicked and execute the actions for that button that many times eventually, when all 3 have been clicked? – Martin Dinov Jan 18 '14 at 11:27
  • I want to execute the actions, after all three buttons have been clicked, regardless of order, or number of times a button has been clicked and at the end send a log to a txt file showing the commands or buttons that has been executed. – Hustl3r28 Jan 18 '14 at 11:31
  • This approach is ok then and will work. The execution of the actions, which can occur in the extra method which you will create, won't happen if you click the same button 3 times, as you won't be incrementing the `int` counter more than once for that button. If you also want to send to the log file the number of times each button was clicked, just keep track of that per button as well. – Martin Dinov Jan 18 '14 at 11:33
  • If this method won't work if i click the same button more than once, how do you suggest i start then? – Hustl3r28 Jan 18 '14 at 11:36
  • It will work even if you click the same button multiple times. See the suggested code for the left button. I hope this makes sense to you. – Martin Dinov Jan 18 '14 at 11:37
  • Alright thanks. But going back to my first problem, how do i make the robot wait for my desired number of buttons to be clicked before it executes the program, rather than moving as soon as i click any button? – Hustl3r28 Jan 18 '14 at 12:05
  • Alright thanks. But going back to my first problem, how do i make the robot wait for my desired number of buttons to be clicked before it executes the program, rather than moving as soon as i click any button? – Hustl3r28 Jan 18 '14 at 19:42
  • ok, i understand the "leftClicked" is the boolean varaible, and the "numButtonsClicked" is the integer, but what do you mean by "countLeftButtonsClicks". please any explanation is appreciated. – Hustl3r28 Jan 24 '14 at 16:25