user3263215
user3263215

Reputation: 63

Clicking a JButton consecutively

Is there a way to know if a JButton was clicked consecutively? Consider my code.

public void actionPerformed(ActionEvent arg0) {
    String bucky[] = new String[2];
    String firstclick = null, secondclick = null;
    clicks++;
    if (clicks == 1) {
        bucky[0] = firstclick;
    } else if(clicks == 2) {
        bucky[1] = secondclick;
        if (bucky[0] == bucky[1]) {
            //This JButton was clicked twice in a row.
        }
    }

This code checks the entire number of times my JButton was clicked and displays the message "This button was clicked twice in a row". What I want is to compare two clicks from that button and see if they come one after the other rather than counting the number of clicks made. Or is there a built-in function that does this?

Upvotes: 1

Views: 979

Answers (3)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285430

I have a different approach to your problem:

  • You want to not allow the user to press the same button in some group of buttons twice in a row.
  • You're solutions so far have tried to check which button was pressed last, and then warn the user if the same button has been pressed in a row.
  • Perhaps a better solution is to create a construct that simply doesn't allow the user to press the same button twice in a row.
  • You can create your ButtonGroup like object that selectively disables the last button pressed, and enables all the other buttons.
  • You would give this class an add(AbstractButton btn) method to allow you to add all the buttons that you wish to behave this way to it. The button would then be added to an ArrayList.
  • You would give it a single ActionListener that listens to all the buttons. Whenever the actionPerformed method has been pressed, it enables all of the buttons, and then selectively disables the last button pressed.

For instance consider my class below:


public class NoRepeatButtonGroup implements ActionListener {
   private List<AbstractButton> btnList = new ArrayList<>();

   public void add(AbstractButton btn) {
      btnList.add(btn);
      btn.addActionListener(this);
   }

   @Override
   public void actionPerformed(ActionEvent evt) {
      for (AbstractButton btn : btnList) {
         btn.setEnabled(true);
      }
      ((AbstractButton) evt.getSource()).setEnabled(false);
   }

   public void reset() {
      for (AbstractButton btn : btnList) {
         btn.setEnabled(true);
      }
   }
}

If you create a single object of this in your class that creates the buttons, and add each button to the object of this class, your code will automatically disable the last button pressed, and re-enable it once another button has been pressed.

You could use it like so:

  JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 5, 0));
  NoRepeatButtonGroup noRepeatButtonGroup = new NoRepeatButtonGroup();

  JButton yesButton = new JButton(new YesAction());
  noRepeatButtonGroup.add(yesButton);
  buttonPanel.add(yesButton);

  JButton noButton = new JButton(new NoAction());
  noRepeatButtonGroup.add(noButton);
  buttonPanel.add(noButton);

  JButton maybeButton = new JButton(new MaybeAction());
  noRepeatButtonGroup.add(maybeButton);
  buttonPanel.add(maybeButton);

For example, here is a proof of concept minimal runnable example:

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;

public class NoneInARowBtns {
   private static void createAndShowGui() {
      JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 5, 0));
      NoRepeatButtonGroup noRepeatButtonGroup = new NoRepeatButtonGroup();

      int buttonCount = 5;
      for (int i = 0; i < buttonCount; i++) {
         JButton btn = new JButton(new ButtonAction(i + 1));
         noRepeatButtonGroup.add(btn);
         buttonPanel.add(btn);
      }

      JOptionPane.showMessageDialog(null, buttonPanel);

   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

@SuppressWarnings("serial")
class ButtonAction extends AbstractAction {
   public ButtonAction(int i) {
      super("Button " + i);
   }

   @Override
   public void actionPerformed(ActionEvent e) {
      System.out.println(e.getActionCommand() + " Pressed");
   }
}

class NoRepeatButtonGroup implements ActionListener {
   private List<AbstractButton> btnList = new ArrayList<>();

   public void add(AbstractButton btn) {
      btnList.add(btn);
      btn.addActionListener(this);
   }

   @Override
   public void actionPerformed(ActionEvent evt) {
      for (AbstractButton btn : btnList) {
         btn.setEnabled(true);
      }
      ((AbstractButton) evt.getSource()).setEnabled(false);
   }

   public void reset() {
      for (AbstractButton btn : btnList) {
         btn.setEnabled(true);
      }
   }
}

When the above program runs, and when the second button is pressed, you will see that it is disabled:

2nd button pressed

Then when the 3rd button has been pressed, the 2nd is re-enabled, and the 3rd one is disabled:

enter image description here

And etc for the 4th button....

enter image description here

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 692171

Just use a field remembering what the last clicked button was:

private JButton lastButtonClicked;

...

someButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        if (lastButtonClicked == e.getSource()) {
            displayError();
        }
        else {
            lastButtonClicked = (JButton) e.getSource();
            doSomething();
        }
    }
});

Of course, you'll have to do the same thing with all the other buttons.

Upvotes: 2

Roberto Anić Banić
Roberto Anić Banić

Reputation: 1421

A global variable arrau of booleans, one for each button, set true on first click, set false or second, sjould do it

Upvotes: 0

Related Questions