Reputation: 29
Im looking to use JButton in substitute of my Keylistener. Key listener has methods such as if pressed if released etc. Im looking to do the same thing with this JButton, Exp: If user clicks with mouse and holds mouse in clicked position code will execute until s/he releases the buttoning which execution of the code will cease.
What I tried? I tried to use JButton at first which didn't produce the result I wanted because form my understanding JButton requires a full "click" I have been playing with JToggleButton if (JToggButton.getModel().isPressed()) which is still not working, can someone please point me in the right direction to produce the desired result?
SPECIFIC GOAL:
I want to use the a microphone method I built, I will click the Button that says Microphone and I will hold down the click until I'm ready to finish speaking into the Mic, think of Facebook how you hold down the mic with your thumb and when you release it the voice recording stops, so there would be 2 methods startLogic(); when pressed down and held and stopLogic(); when finally released when user is done speaking
Upvotes: 0
Views: 4480
Reputation: 285405
Note that a simple but wrong solution is to use a MouseListener, wrong since this listener does not respond to button depression but rather mouse press, and this will miss button press if it is pressed by any means other than the mouse, such as the spacebar.
I would listen to the button's ButtonModel with a ChangeListener and respond to changes in its isPressed()
state. This will work no matter what presses the button, be it the mouse or the spacebar. For example:
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
@SuppressWarnings("serial")
public class ButtonPressTest extends JPanel {
private JButton button = new JButton("Button");
private JTextArea textArea = new JTextArea(15, 15);
public ButtonPressTest() {
button.getModel().addChangeListener(new BtnModelListener());
textArea.setFocusable(false);
add(button);
add(new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));
}
private class BtnModelListener implements ChangeListener {
private boolean pressed = false; // holds the last pressed state of the button
@Override
public void stateChanged(ChangeEvent e) {
ButtonModel model = (ButtonModel) e.getSource();
// if the current state differs from the previous state
if (model.isPressed() != pressed) {
String text = "Button pressed: " + model.isPressed() + "\n";
textArea.append(text);
pressed = model.isPressed();
}
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame("ButtonPressTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new ButtonPressTest());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Upvotes: 6