Java detects long mouse press
5 answers
You can set a timer in your mouseDown event listener and execute it every 500ms after an initial delay of 3000ms. In your mouse, you can override this timer. In the run method of the object TimerTask
associated with yours Timer
, you can perform the computation of the required task. Here is my solution:
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class Test
{
public static void main(String[] args)
{
final JFrame f = new JFrame();
String[] data = {"one", "two", "three", "four"};
JList myList = new JList(data);
f.add(myList);
myList.addMouseListener(
new MouseAdapter()
{
private java.util.Timer t;
public void mousePressed(MouseEvent e)
{
if(t == null)
{
t = new java.util.Timer();
}
t.schedule(new TimerTask()
{
public void run()
{
System.out.println("My importan task goes here");
}
},3000,500);
}
public void mouseReleased(MouseEvent e)
{
if(t != null)
{
t.cancel();
t = null;
}
}
}
);
f.pack();
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
f.setVisible(true);
}
}
);
}
}
+4
source to share
Here's what I use for situations like this:
public class Player implements Runnable, MouseListener
{
public void mousePressed(MouseEvent e)
{
holding = true;
thread = new Thread(this);
thread.start();
}
public void mouseReleased(MouseEvent e)
{
holding = false;
System.out.println("Held for: "+seconds);
}
public void mouseClicked(MouseEvent e){}
public void run()
{
try
{
while(holding)
{
seconds++;
// put some code here
if(seconds==3)
{
holding = false;
System.out.println("Held for maximum time!");
}
}
}catch(Exception e){e.printStackTrace();}
private boolean holding;
private int seconds;
private Thread thread;
}
add this to your JLabel by calling label.addMouseListener (new Player ());
+3
source to share
Two different solutions (almost the same as the previous ones):
new MouseAdapter() {
Date pressedTime;
long timeClicked;
@Override
public void mousePressed(MouseEvent e) {
pressedTime = new Date();
}
@Override
public void mouseReleased(MouseEvent e) {
timeClicked = new Date().getTime() - pressedTime.getTime();
if (timeClicked >= 3000) {
// DO YOUR ACTION HERE
}
}
};
or
new MouseAdapter() {
boolean mousePressed = false;
@Override
public void mousePressed(MouseEvent e) {
new Thread(new Runnable() {
@Override
public void run() {
mousePressed = true;
while (mousePressed) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (mousePressed) {
// DO YOUR ACTION HERE
}
}
}
}).start();
}
@Override
public void mouseReleased(MouseEvent e) {
mousePressed = false;
}
}
+3
source to share
With this code, you can detect and control a long press or a short press (like a click), one event excludes the other.
private int pressStatus = 0; // TO DETECT IF LONG IS REAL LONG OR SHORT PRESSED
private Timer t;
...
@Override
public void mousePressed(final MouseEvent arg0) {
pressStatus = 0; // to manage simple click or long
if (t == null) {
t = new Timer();
}
t.schedule(new TimerTask() {
public void run() {
pressStatus = 1;
if (t != null) {
t.cancel();
t = null;
}
// PRESSED LONG
int modifiers = arg0.getModifiers();
if ((modifiers & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) {
...
} else if ((modifiers & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) {
....
}
}
}, 700, 1);
}
@Override
public void mouseReleased(MouseEvent arg0) {
if (t != null) {
t.cancel();
t = null;
}
// PRESSED SHORT LIKE CLICK
if (pressStatus == 0) {
int modifiers = arg0.getModifiers();
if ((modifiers & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) {
...
} else if ((modifiers & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) {
...
}
}
pressStatus = 0;
}
0
source to share