2

I was wondering if there was a built in function in java or a way to check if a JButton is clicked twice in a row. This is not to be confused with keeping track of how many times a JButton has been clicked or double clicking a JButton. I have searched multiple threads and they all seem to be talking about the latter.

I have the following code that works if a button called button1 is clicked once. I need a bit of help tweaking it to work for when the button is clicked twice i.e, back to back.

public void actionPerformed(ActionEvent arg0){
    JButton button1 = (JButton) arg0.getSource();
    if (button1 == button1) {
        //You clicked button1 twice in a row
    }
}
takendarkk
  • 3,347
  • 8
  • 25
  • 37
Hustl3r28
  • 211
  • 7
  • 16
  • Can you put a little more context into what you're trying to achieve with this? – Paul Samsotha Feb 01 '14 at 10:54
  • You could take a look at this [example](http://stackoverflow.com/questions/21441243/event-listeners-and-mouse-listeners/21442137#21442137) which was trying to trigger a method on a double "action" of a button, but you could use a simular concept to "trap" multiple clicks until a specific period of time has past – MadProgrammer Feb 01 '14 at 10:58
  • @Madprogrammer, I have seen that thread before. It doesn't solve my problem but thanks anyway. – Hustl3r28 Feb 01 '14 at 11:00
  • `button1 == button1` is a meaningless always true condition. – DSquare Feb 01 '14 at 18:22
  • Is there a built in function, no. The basic concept is you need to place a short delay into the process, so that if the button is triggered again before the delay expires, it's being double clicked... – MadProgrammer Feb 01 '14 at 19:13

1 Answers1

2

More context would be nice, but you can easily make a way to track what buttons has been pressed storing the information you need (last button pressed instance, a counter of how many times was pressed, or an array with the last events...). Something like:

private JButton _lastButtonPressed;    

void actionPerformed(ActionEvent event)
{
    JButton buttonPressed = (JButton) event.getSource();
    if (_lastButtonPressed == buttonPressed)
    {
        //The same button was clicked two+ times in a row
    }
    else
    {
        //code for handling single button presses
    }
    _lastButtonPressed = buttonPressed;
}

UPDATE

Here's a simple executable program that makes use of the code above. The actionListener is able to know how many times in a row a Button has been pressed.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class ButtonTestFrame implements ActionListener
{
    public static final void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new ButtonTestFrame();
            }
        });
    }

    public ButtonTestFrame()
    {
        _frame = new JFrame();

        _frame.setTitle("Button test frame");

        _textArea = new JTextArea("Press some buttons:\n");
        _textArea.setEditable(false);
        _frame.add(new JScrollPane(_textArea));

        JPanel buttonsPanel = new JPanel(new GridLayout(1, 3, 1, 1));
        _buttonA = new JButton("Button A");
        _buttonA.addActionListener(this);
        _buttonB = new JButton("Button B");
        _buttonB.addActionListener(this);
        _buttonC = new JButton("Button C");
        _buttonC.addActionListener(this);
        buttonsPanel.add(_buttonA);
        buttonsPanel.add(_buttonB);
        buttonsPanel.add(_buttonC);

        _frame.add(buttonsPanel, BorderLayout.SOUTH);

        _frame.setPreferredSize(new Dimension(600,400));
        _frame.pack();
        _frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        _frame.setLocationByPlatform(true);
        _frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent event)
    {
        JButton buttonPressed = (JButton) event.getSource();

        if (_lastButtonPressed == buttonPressed)
        {
            //The same button was clicked two+ times in a row
            count++;
            _textArea.append(buttonPressed.getText() + " has been pressed " + count + " times in a row.\n");
        }
        else
        {
            //code for handling single button presses
            count = 1;
            _textArea.append(buttonPressed.getText() + " has been pressed.\n");
        }
        _lastButtonPressed = buttonPressed;
    }

    private int count = 0;
    private JButton _lastButtonPressed;
    private JButton _buttonA;
    private JButton _buttonB;
    private JButton _buttonC;
    private JTextArea _textArea;
    private JFrame _frame;
}

Result:

Button test frame

DSquare
  • 2,458
  • 17
  • 19
  • like I said in my question, I don't want to track the button that has been pressed, as I already know which one I am going to press. All I want is to see if a button is pressed back to back or twice in a row. Is there a function in Java to do this? – Hustl3r28 Feb 01 '14 at 15:53
  • @Hustl3r28 To know when a button has been pressed twice in a row you can use my code above. If you don't want to know when a button has been pressed twice in a row you'll have to explain yourself better. – DSquare Feb 01 '14 at 16:21
  • Does it give you a DoesNotWorkException? How is it not working? That code sure works, and it can be easily modified to get whatever particular functionality you want. Can't read your mind and you don't seem willing to have a proper iscussion so good luck with that. – DSquare Feb 01 '14 at 17:07
  • OK let me edit my initial question and make the situation more clearer. – Hustl3r28 Feb 01 '14 at 18:02
  • what does the variable _lastButtonPressed mean? – Hustl3r28 Feb 01 '14 at 18:25
  • @Hustl3r28 For this answer to work, your buttons must all have the same listener. – Radiodef Feb 01 '14 at 18:26
  • I am only checking the back to black click for one button, not all of my JButtons. – Hustl3r28 Feb 01 '14 at 18:31
  • Hustl3r28 it contains the last intance of a button that has been pressed, it's assigned in the last line of the acitonPerformed method; @Radiodef typically yes but the actual restriction is that all the listener must have access to the shared memory in this case _lastButtonPressed. I'll update my answer with an executable example in a moment. – DSquare Feb 01 '14 at 18:33
  • @Hustl3r28 However, when it's not a back to back click, that would imply that a *different* button has been pressed. So you need to keep track of the other buttons. – Radiodef Feb 01 '14 at 18:35
  • So, is it safe to assume that buttonPressed is the name of the JButton in question? – Hustl3r28 Feb 01 '14 at 18:43
  • No buttonPressed is a variable created in the actionListener at which I assign the reference to the button that was just pressed with the line `JButton buttonPressed = (JButton) event.getSource();`. The source of the event is the button that was pressed, whichever it is. – DSquare Feb 01 '14 at 18:46