Reputation: 211
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.
Upvotes: 0
Views: 492
Reputation: 205875
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.
Upvotes: 1
Reputation: 8825
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();
}
}
Upvotes: 0
Reputation: 12890
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
Upvotes: 1