Java MouseEvent, check if button is clicked

I have a class that implements MouseListener (JPanel). When I click on the panel, something happens. What I want is some kind of while loop that loops while the left mouse button is pressed.

@Override public void mousePressed(MouseEvent e) { while(e.isPressedDownD) { // <-- //DO SOMETHING } } 

This clearly does not work, but I hope you understand what I'm trying to achieve. The whole class for those interested:

 package control; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import model.GridModel; import view.GUIView; public class MapListener implements MouseListener{ private GridModel model; private GUIView view; private int posX; private int posY; public MapListener(GridModel model, GUIView view) { this.model = model; this.view = view; } @Override public void mouseClicked(MouseEvent e) { posX = e.getX(); posY = e.getY(); model.setMouseAtX(posX); model.setMouseAtY(posY); view.paintTile(); System.out.println("X: " + posX + " Y: " + posY); } @Override public void mouseEntered(MouseEvent e) { } @Override public void mouseExited(MouseEvent arg0) { } @Override public void mousePressed(MouseEvent e) { while(e.getModifiers() == MouseEvent.MOUSE_PRESSED) { //Obviously doesn't work //DO SOMETHING } } @Override public void mouseReleased(MouseEvent arg0) { } } 
+6
source share
5 answers

As pointed out in other answers, the place for your work is not in the methods of listening for mouse events.

There is also no explicit “mouse” concept in MouseEvent , so you have to track this yourself. I gave an example of how to do this. Also pay attention to the MouseEvent.BUTTON1 links, as it is easy to track the state of the left mouse button.

Here you should start to learn about concurrency. For this reason, I added a synchronized method, since you need to know that funny things happen when access properties for multiple threads are applied at the same time, and synchronized is a mechanism to maintain this normal functioning. Consider it further outside of this example.

Unconfirmed, but this should work:

 volatile private boolean mouseDown = false; public void mousePressed(MouseEvent e) { if (e.getButton() == MouseEvent.BUTTON1) { mouseDown = true; initThread(); } } public void mouseReleased(MouseEvent e) { if (e.getButton() == MouseEvent.BUTTON1) { mouseDown = false; } } volatile private boolean isRunning = false; private synchronized boolean checkAndMark() { if (isRunning) return false; isRunning = true; return true; } private void initThread() { if (checkAndMark()) { new Thread() { public void run() { do { //do something } while (mouseDown); isRunning = false; } }.start(); } } 
+14
source

Why do many of these answers erroneously claim that MouseEvent does not have an explicit “mouse” concept?

Although other commentators are correct that the OP should not do all this in an event handler, there are other situations where querying the state of a button in a mouse listener is useful. In these cases, you MAY actually determine the state of the button. For instance:

 @Override public void mouseExited(MouseEvent event) // Or any other mouse event handler... { int buttonsDownMask = MouseEvent.BUTTON1_DOWN_MASK | MouseEvent.BUTTON2_DOWN_MASK | MouseEvent.BUTTON3_DOWN_MASK; // Or whichever buttons you care about... if ( (event.getModifiersEx() & buttonsDownMask) != 0 ) System.out.println("Hey! Some button is pressed!"); } 

Note in particular the use of the MouseEvent.getModifiersEx() method together with MouseEvent.BUTTON1_DOWN_MASK and friends.

+9
source

You can create a new thread containing a while loop. You start this topic when you click the mouse. You stop it when you release the mouse button.

+4
source

You should not do this in the event handler, since no more events will be processed until the event handler exits.

What you want to achieve can be accomplished using a separate workflow. Create a stream from the mousePressed , do what you want to do in the stream (this should contain a while loop), and make the stream complete when the mouse is released (your mouseReleased listener should notify the stream).

+3
source

eg

 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ClickListener extends MouseAdapter implements ActionListener { private final static int clickInterval = (Integer) Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval"); private MouseEvent lastEvent; private Timer timer; public ClickListener() { this(clickInterval); } public ClickListener(int delay) { timer = new Timer(delay, this); } @Override public void mouseClicked(MouseEvent e) { /*if (e.getClickCount() > 2) { return; } lastEvent = e; if (timer.isRunning()) { timer.stop(); doubleClick(lastEvent); } else { timer.restart(); }*/ if (timer.isRunning() && !e.isConsumed() && e.getClickCount() > 1) { System.out.println("double"); timer.stop(); } else { timer.restart(); } } @Override public void actionPerformed(ActionEvent e) { timer.stop(); singleClick(lastEvent); } public void singleClick(MouseEvent e) { } public void doubleClick(MouseEvent e) { } public static void main(String[] args) { JFrame frame = new JFrame("Double Click Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.addMouseListener(new ClickListener() { @Override public void singleClick(MouseEvent e) { System.out.println("single"); } @Override public void doubleClick(MouseEvent e) { System.out.println("double"); } }); frame.setPreferredSize(new Dimension(200, 200)); frame.pack(); frame.setVisible(true); } } 
+2
source

Source: https://habr.com/ru/post/893581/


All Articles