I would like to detect mouse moving, while the left button is pressed.
I simply create a drawing application. I can detect the mouse move without any mouse pressed. But I want to detect WITH left mouse pressed.
I think there is not any listener for this. So, what is the idea to do that?
component.addmouseMotionListener(new MouseAdapter() {
public void mouseDragged(MouseEvent evt) {
if ( SwingUtilities.isLeftMouseButton(evt)) {
// do your stuff here
}
}
});
there is mouseDragged method in MouseMotionListener...check this out for more details...
In your MouseMotionListener you can check for this using:
SwingUtilities.isLeftMouseButton(me.getPoint())
and you would listen for the mouseDragged event.
I think there might be some sort of mouse drag listener, but if not...
Capture the mouse button down and up events. In those events, set a boolean such as leftButtonIsDown = true (unless there's already some easy way to query that) and then in the mouse movement events use a block like if (leftButtonIsDown)
Related
How can I check if currently any mouse button is pressed and if so, which one it is?
The thing is that I need to use this kind of information in MouseListener.mouseEntered(). I checked MouseEvent but I could not find the method which would help me.
The getButton() method seems to only return value if there has been change in buttons' state.
Is there a way to find this out without manually keeping track of this somehow vie MouseListener.mousePressed()/mouseReleased() methods.
How can I check if currently any mouse button is pressed and if so, which one it is?
Presumably you want to invoke specific code depending on the button pressed so you can do something like:
if (SwingUtilities.isLeftMouseButton(...))
// do something
You could start by looking at How to write a Mouse Listener and the JavaDocs for MouseEvent in particular, the getButton method.
However, there are cross platform considerations that need to taken into consideration, which are overed by SwingUtilities.isLeftMouseButton and equivalent methods...
This will solve your problem
long eventMask = AWTEvent.MOUSE_MOTION_EVENT_MASK + AWTEvent.MOUSE_EVENT_MASK;
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
public void eventDispatched(AWTEvent e) {
System.out.println(e.paramString()+"-"+e.getSource());
}
}, eventMask);
This is a Global Event Listeners.
Get the source and button from AWTEvent and do whatever you want to perform.
I was wondering, how does a Swing component know where the mouse is and when it is clicked, and how could I use that in my own class, without having to add a new mouse listener every time i want to add an object to a new panel?
EDIT:
Im extending JComponent and I want to get an event method called when the mouse moves
EDIT2:
Got it working now thanks everyone!
Add an actionlistener to your JButton and it will tell you when its been clicked like so:
someButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
//the button was pressed and do your stuff here.
}
}
how does the JButton know where the mouse is and when it clicked
thats what for Listeners are - it listens for a corresponding type of event
just implement ActionListener and register it to its listener by doing this:
jbutton.addActionListener(this);
now when you click the button it will generate an event which will b handled in this part
public void actionPerformed(ActionEvent e){
... // handle event
}
From the comments of the other post, you want to register a MouseListener to you custom component using Component#addMouseListener
You may like to have a read through How to write a mouse listener for more info
I've been looking at how to programmatically set the position of the cursor. Doing some googling I found the use of the Robot class. But when I do this it calls the mouseMoved event implemented in MouseMotionListener which I don't want. Are there any other ways to set the position that wouldn't call that method?
The mouseMoved event will still fire no matter what you do, but you can overwrite it, so that it does nothing once fired.
You can overwrite the listener of the component that you are moving the mouse on, so that only that component will ignore the event but others components will trigger properly.
myComponent.addMouseMotionListener(new MouseMotionAdapter()
{
#Override
public void mouseMoved(MouseEvent e)
{
/*Do Nothing*/
}
});
I'm trying to do the MouseOver effect like it is known in JavaScript in Java for a JButton. I added a MouseMotionListener and it worked. I did want to set 2 other Buttons visible, if my mouse touches the 1st Button. So that works perfectly.. but I don´t know how to handle if the mouse isn´t over the Button. I want to setVisible the Buttons false after the Mouse left the Button
Heres my code:
mouseover.addMouseMotionListener(new MouseMotionListener() {
public void mouseDragged(MouseEvent arg0) {}
public void mouseMoved(MouseEvent arg0) {
del.setVisible(true);
addone.setVisible(true);
}
mouseover is the Button I want to listen to.
del is another Button wich I want to setVisible
addone also
Sry for my not really awesome english :P
Thank you !
You're looking for a MouseListener, specifically implementing mouseExited.
http://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html
You may want to use MouseAdapter to avoid being forced to implement all of the methods from MouseListener. MouseAdapter is simply a class that implements the mouse listening interfaces.
http://docs.oracle.com/javase/6/docs/api/java/awt/event/MouseAdapter.html
Perhaps you want to check setRolloverIcon(), setRolloverSelectedIcon() methods, instead of using MouseEvent.
Instead of using a MouseMotionListener. Use a MouseListener, this class has two methods called mouseEntered() and mouseExited() these should allow you to make the necessary changes as the mouse comes in and out of the button.
Here is a brief tutorial on MouseListeners
Is mouseMotionListener going to trigger an event once the mouse moves over the component, whereas mouseListener only triggers if I press a button?
So if I have only a mousePressed event, then I don't need a mouseMotionListener? Only if I had a mouseEntered or mouseExited?
Yes, you are correct. mouseMotionListener is used to perform actions when your mouse moves over a "hotspot"
Good example can be found here
When dealing with mousePressed events, you only need mousePressed events unless you wanted to add more events to perform while the mouse is hovered.
They listen for different events:
MouseListener
mouseClicked(MouseEvent event) // Called just after the user clicks the listened-to component.
mouseEntered(MouseEvent event) // Called just after the cursor enters the bounds of the listened-to component.
mouseExited(MouseEvent event) // Called just after the cursor exits the bounds of the listened-to component.
mousePressed(MouseEvent event) // Called just after the user presses a mouse button while the cursor is over the listened-to component.
mouseReleased(MouseEvent event) // Called just after the user releases a mouse button after a mouse press over the listened-to component
MouseMotionListener
mouseDragged(MouseEvent event) // Called in response to the user moving the mouse while holding a mouse button down. This event is fired by the component that fired the most recent mouse-pressed event, even if the cursor is no longer over that component.
mouseMoved(MouseEvent event) // Called in response to the user moving the mouse with no mouse buttons pressed. This event is fired by the component that's currently under the cursor.
Add the listeners according to what event you're after.