Removing MouseListener() from a JLabel - java

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)

Related

How can I listen my all components with only one event?

Here is the problem,
I have 3 textfield, 3 button and 1 label. Their text are text1, text2 text3, but1,but2,but3.
I give you an example about what I want to do; When I double click on a button, button will change label's text as button's text. I mean when I double click to but2, label's text should be but2.
I can do this with that code;
MouseAdapter ml = new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
if (me.getClickCount()==2) {
jLabel1.setText(jButton1.getText);
}
}
};
jButton1.addMouseListener(ml);
So It works but it works only for jButton1. I have to write different mouseListener's for all components(textfields and jbuttons). How can I do this with one listener? or one event ? Do you have any idea?
Take a look at MouseEvent#getSource ... although, to be honest, if you're using JButtons you shouldn't be using a MouseListener, but ActionListener instead. Also, generally speaking, most users won't double click a JButton as it's not intuitive for them to do so, buttons only need a single click to activate
Remember, buttons can be activated by the keyboard as well, which MouseListener won't be notified about

Mouse event handling in Swing

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

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.

Java MouseEvents not working

This may be a stupid question, but I have to ask!
I have the following code snippets that are supposed to run their corresponding methods when the user interacts with objects.
For some reason, "foo" is never printed, but "bar" is.
myJSpinner1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
System.out.println("foo"); //"foo" is not printed
}
});
myJSpinner2.addChangeListener(new java.awt.event.ChangeListener() {
public void stateChanged(java.awt.event.ChangeEvent evt) {
System.out.println("bar"); //"bar" is printed
}
});
I get no exceptions or stack trace. What am I missing in the MouseListener one?
Thanks in advance.
EDIT: MouseEntered works perfectly on a JCheckBox implemented in exactly the same way!
JSpinner is a composite component consisting of a text field and 2 buttons. It's possible to add mouse listeners to all of those by iterating over the results of getComponents() and adding a listener to each.
However, in my experience, when something takes that much work, you're probably going about it the wrong way.
Why do you need the mouse-entered information for a JSpinner?
What do you want to do with this event?
Update:
If you're looking to supply information about all of the controls in your panel, you may want to look at using a glasspane to detect the component under the mouse.
A Well-behaved Glasspane by Alexander Potochkin is a good place to start.
This is a guess but I suspect you need to add a MouseListener to the JSpinner's editor (via a call to getEditor()). I imagine that the editor Component occupies all available space within the JSpinner and is therefore intercepting all MouseEvents.
This worked for me.
JSpinner spinner = new JSpinner();
((JSpinner.DefaultEditor)spinner.getEditor()).getTextField().addMouseListener(
new java.awt.event.MouseAdapter() {
public void mouseClicked(final MouseEvent e) {
// add code here
}
});
I needed this in order to evoke a popup key dialog for added usability due to our software requirements.
Aditional to #Rapier answer...
If you change the Spinner using something like
yourOldSpinner = new JSpinner(new SpinnerModel(...))
you will lost your previosly MouseListener...
If you need to change something of SpinnerModel, Don't create a new, change its parameters instead! (if you do it, you will need to reassign the MouseListener again, because it will be lost when you assign a new SpinnerModel).
an example (I'm talking...):
((SpinnerNumberModel)yourOldSpinner.getModel()).setValue(size/3);
((SpinnerNumberModel)yourOldSpinner.getModel()).setMinimum(0);
((SpinnerNumberModel)yourOldSpinner.getModel()).setMaximum(isize/2);
((SpinnerNumberModel)yourOldSpinner.getModel()).setStepSize(1);

Categories

Resources