Mouse event handling in Swing - java

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

Related

How to display Image when button1 is pressed

How can i display and image when button1 is pressed, this is the code im using to check for a button press:
public class clientFrameButtons {
public void frameClient(){........
....
button1.addActionListener(new ActionListener(){ //ActionListener checks for button press
public void actionPerformed(ActionEvent e){
// if button is pressed the following will happen
I've tried implementing an if statement and looked at several sources online but cannot find a answer to fit my needs.
Thanks.
ActionEvent is a logical event those intention is to signal the fact that a button was actioned. So it doesn't provide any information about the user inputs that have lead to the action (it may be a shortcut, a mouse press, your mouse could have been set to left-handed, etc). If you want to care about mouse, then you must catch mouse events, with an appropriate MouseListener.
So your problem can be expressed in two ways:
you want to display an image when a GUI button is actioned then use ActionListener.
you want to make different things depending on the mouse button pressed while over a GUI button, then use MouseListener.

What is the difference between ActionListener and ActionEvent for a button?

I know this is a really noob of a question but I have recently started to learn Java. I am not really sure what does an ActionEvent do and what does the Action listener does.
Let's take a Shop owner in his/her shop monitoring his/her employees.
If the shop owner arrived to the shop, he will start monitoring his employees(which means he has started to observe them waiting for an Event to happen, which is what an ActionListener does).
If the shop owner catches an employee sleeping on his shift, he will take action to the event(ActionEvent), perhaps firing them or giving them a warning(Action).
now back to java.
To declare an actionListener in the class, you either implement ActionListener (ActionListener is an Interface ) and add the method :
public void actionPerformed(ActionEvent e){
}
or you can add the actionListener directly to the JComponent like :
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
// action goes here.
}
})
An ActionListener listens for ActionEvents. An ActionEvent is something that happens; an ActionListener is something that responds to it.
An ActionListener is an object listening for an action to occur, such as something being clicked.
An ActionEvent is the event that occurred. So say you click on a JButton, an ActionEvent is fired containing the object that the event occurred to, and some other info.
Hope this helps, good luck!
An ActionEvent is an object that represents the event that occurs when a user clicks on a button.
An ActionListener is an interface that defines the (actionPerformed()), which handles ActionEvents
hope this makes sense
A ActionEvent is send to a ActionListener. For example: If you have a button, and this button is being pressed. A signal form the button will be sent as a ActionEvent. If you then have a ActionListener for that button, that event can be handled by the ActionListener for that button.

Removing MouseListener() from a JLabel

I added a MouseListener to a JLabel. Now if I want to disable this MouseListener associated with the JLabel, when the label is clicked once, how can I do it.
I know there is a big way to set a boolean or int variable when the label is clicked and then call a method and remove MouseListener there, but I want to learn a compact and easy way. Is there a way to do this?
In your mouse listener:
public void mouseClicked(MouseEvent event) {
// Do stuff...
((Component) event.getSource()).removeMouseListener(this);
}
What's wrong with label.removeMouseListener(listener)? It works just fine. If you want to create listener that removes itself call label.removeMouseListener(this)

Java JButton - MouseMotionListener ( MouseMoved ) MouseOver Effect

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

button event still works with button disabled

private void button_Clicked_download(MouseEvent e) {
button_dl.setEnabled(false);
System.out.println("Button Clicked.");
}
When I click the button the button looks disabled. However the button still executes the code under the MouseEvent and I see "Button Clicked." in the debug console.
How can I make it so if the button is clicked it ignores the code and is indeed disabled?
However the button still executes the code under the MouseEvent and I see "Button Clicked." in the debug console.
This is exactly why you shouldn't use a MouseListener with a JButton but rather an ActionListener. The solution is of course obvious -- to get rid of the MouseListener and instead add an ActionListener to the JButton of interest.
you need to use an ActionListener instead of MouseClickListener.
your button is logically clicked even if it is disabled so click event will execute
There is actually a very simple way of enabling and disabling a button in java which uses a Mouse Listener.
class HoldListen extends MouseAdapter {
#Override
public void mousePressed(MouseEvent e) {
JButton bt = (JButton)e.getSource();
if (!bt.isEnabled()) {
return;
}
// Do code
}
}
I found your question while trying to create something similar and this is how I solved it.
All the MouseListener's methods return void so it works out quite nice. In my situation going back to an ActionListener would have required a lot of extra work while a MouseListener was perfect for the job. Press set a variable which Release undid and another thread used the variable in a ongoing simulation.

Categories

Resources