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
Related
I am trying to add a mouse listener to a MapMarker, so that when a mouse will hover a MapMarker, I could react with an event.
I implemented the mouseListener, but I can't really add a listener.
The issue is that I did not find a way the MapMarker will addMouseListener, due to the fact that non of the hierarchy implements JPanel.
Any help appreciated
As noted here, the default JMapViewer constructor uses a DefaultMapController, "which implements map moving by pressing the right mouse button and zooming by double click or by mouse wheel." To see map coordinates, extend DefaultMapController, override mouseClicked() and use one of the viewer's getPosition() methods. You can use your controller as shown in comments at line 65 of Demo.java, r30377.
new DefaultMapController(map){
#Override
public void mouseClicked(MouseEvent e) {
System.out.println(map.getPosition(e.getPoint()));
}
};
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 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)
I would like to know if there is any way to add an ActionListener to a JPanel?
I have no problem adding those to JButtons, but JPanel seems not to have such a method.
Basically I have a bunch of JPanels inside a JFrame with a grid layout and I would like to know if there is any way to know when the user has clicked on one of them.
Any help much appreciated!
An action listener serves to listen to action events. A button triggers an action event when it's pressed and released using the mouse (and not when it's clicked), or when it's pressed using the keyboard (space bar, mnemonic, keyboard shortcut, etc.). It's a high-level event. A mouse click is a more low-level event, which is handled by a MouseListener.
Add a MouseListener to your JPanel if you want to handle mouse clicks.
Aparently you can use AddMouseLitener..
Silly me..
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.