mouseEntered events on layers of JComponents on a JPanel

My program has a JPanel that displays custom JComponents. The user hits a button and it creates more JComponents. My problem is that I need to know when these JComponents on the panel overlap. I tried doing this be having the JComponents listen for mouseEntered events and check whether a component was currently selected (field set when the mouse is pressed on a component, set to null at release time).

My problem is that when you drag a newer component over an older one it works, but when you drag an older component over a newer one no mouseEntered event is registered for the older one.

I need it to work both ways, regardless of which order the components were created in by the user.

Thanks in advance for suggestions.

[752 byte] By [johnd225a] at [2007-11-26 17:22:20]
# 1

I'm not sure I quite understand why you're relying on mouseEntered. Could you post some code (a [url http://homepage1.nifty.com/algafield/sscce.html]SSCCE[/url] is preferrable) or elaborate more on exactly what the problem code is trying to accomplish.

I'm assuming you're just trying to make the dragged component show above the other components? If that's the case, change the Z-order for the component by calling setComponentZOrder on the container (Java >= 5.0). Otherwise, you can use a temporary glass pane for dragging. Technically, if you're using Java < 5.0, you should be using a JLayeredPane which supports Z order.

Cheers

Message was edited by:

Jaspre

Jasprea at 2007-7-8 23:50:18 > top of Java-index,Desktop,Core GUI APIs...
# 2

sure no problem, let me put it another way. I don't care how it is achieved, I just need the following one way or another:

The user must be able to drag custome jcomponents (that are created at various points during runtime) around a jpanel. Somehow, like by an event, I need to know when one custom jcomponent is dragged over/under/whatever another custom jcomponent on the jpanel. The application is all doing particular things when the components overlap.

Would you suggest a different way of doing this?

johnd225a at 2007-7-8 23:50:18 > top of Java-index,Desktop,Core GUI APIs...
# 3

In your mouseDragged event handler, do something like this:

Rectangle b = dragging.getBounds();

for (Component c : container.getComponents()) {

if (c != dragging && c.getBounds().intersects(b)) {

System.out.println(c);

// obviously, you'll want to something else here

}

}

Jasprea at 2007-7-8 23:50:18 > top of Java-index,Desktop,Core GUI APIs...
# 4

I'd suggest using a ComponentListener rather than a MouseListener. Otherwise if you move a component by non-mouse means (keyboard shortcuts, undo actions, buttons, other code, etc) you'll be firefighting. Listening to the Component being moved is logically what you're doing, and ComponentListener is the mechanism for this.

itchyscratchya at 2007-7-8 23:50:18 > top of Java-index,Desktop,Core GUI APIs...
# 5
> Listening to the Component being moved is logically> what you're doing, and ComponentListener is the> mechanism for this.Good point.
Jasprea at 2007-7-8 23:50:18 > top of Java-index,Desktop,Core GUI APIs...