Reputation: 433
here is my problem: i have a jpanel that has three panels in it. a verticalseperator panel, a chatouput panel and a chatinput panel. the main jpanel, chatpanel, implements mousemotionlistener, and has the following code:
public void mouseMoved(MouseEvent e) {
int y = e.getY();
//change cursor look if hovering over vertical spacer
if(y >= (int)(verticalPanelSeperator.getLocation().getY()) && y <= (int)(verticalPanelSeperator.getLocation().getY() + verticalPanelSeperator.getHeight())) {
setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
}
else {
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR ));
}
}
public void mouseDragged(MouseEvent e) {
//vertical spacer can be draged up/down to change height of input/output areas
if(getCursor().getType() == Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR).getType()) {
int edge = (int)this.getLocation().getY();
int cur = e.getY();
int height = this.getHeight();
output.setHeightPercent((((double)(cur-edge))/height));
output.componentResized(new ComponentEvent(this, 1));
input.setHeightPercent((((double)(height-cur-edge))/height));
input.componentResized(new ComponentEvent(this, 2));
e.consume();
}
}
the problem is that for some reason, when you move the mouse over the verticalpanelseperator, it changes to a resize cursor, but as you move it up, without dragging, it stays as resize cursor. furthermore, from the print statement, as the mouse goes up into the chatoutput, e.getY()'s value does not increment, it stays within the bounds of the verticalseperatorpanel. its like the chatoutput default mouselistener is overriding the one on the chatpanel and doing nothing. can someone tell me whats going on here and how to fix this?
Upvotes: 0
Views: 264
Reputation: 347332
A MouseListener
will only respond to mouse events for its container, so long as no other containers are monitoring mouse events above it. Think of a mouse event as a rain drop. If you put an umbrella up between you and the rain drop, the rain drop won't reach you.
I'd suggest you should be using mouseEnter
and mouseExit
from the MouseListener
interface instead and registering it directly on the verticalPanelSeperator
Upvotes: 1