Java MouseEvents not working
Asked Answered
M

4

3

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!

Matchmark answered 31/3, 2010 at 11:37 Comment(1)
I realise this explanation is a little sparse. Please ask if you need an extra information. :)Matchmark
M
4

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.

Matrona answered 31/3, 2010 at 13:44 Comment(4)
I have another component in which I want to display text which explains what the spinner does. Like a statusbar, I suppose!Matchmark
Perhaps consider using a tooltip instead of showing the function in the status bar?Rhombohedral
I had considered that, but I need to use tooltips elsewhere for other reasons and for the sake on consistency, I'l rather use this. Surely this is something stupid that I've missed out? I can't see why it would work on other components and not spinners?Matchmark
Iterating over the results of getComponents() works, but only for the buttons, not the text field! Weird, but it'll have to do. Thanks.Matchmark
R
2

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.

Rhombohedral answered 31/3, 2010 at 11:50 Comment(3)
Thanks for your answer. Is this what you mean? senseSpinner.getEditor().addMouseListener(new MouseAdapter(){...})' That doesn't seem to work either. The funny thing is, this code was generated using NetBeans and so in theory, should work!Matchmark
Yes that's what I meant. Actually, you could try adding the listener to the editor's text field by calling ((JSpinner.DefaultEditor)spinner.getEditor()).getTextField().addMouseListener(...).Rhombohedral
Nope, same thing happens. Namely, nothing!Matchmark
R
2

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.

Rozina answered 7/10, 2011 at 19:56 Comment(0)
E
0

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);
Estep answered 30/6, 2016 at 8:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.