I have a project set up using a JLayeredPane with a number of JLabel objects. Some are gray images, and others are images of cards. The cards are drag-able, but I'm trying to make them so they can only be dropped onto the blank spots, if its not one of the blank spots then it snaps back.

The problem I am having is that I cant figure out how to get the component that is under the card that is being dragged.

This is what I have tried in the mouseReleased method in a mouse listener

public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		JLabel c = (JLabel)Global.window.getComponentAt(e.getXOnScreen()-x, e.getYOnScreen()-y);
	
		System.out.println((e.getXOnScreen()) + ","+(e.getYOnScreen()) + " "+c.getName());	//debug
		
		if (c.getName() == "blankLeft2" && card == cardTY.ace)
			{
			//snap  card to this spot
			setBounds(c.getX(),c.getY(),super.getIcon().getIconWidth(), super.getIcon().getIconHeight());
			}

		else if (c.getName() == "blank")		//snap to blank spot
			setBounds(c.getX(),c.getY(), super.getIcon().getIconWidth(), super.getIcon().getIconHeight());

		else //set back to old spot spot
			setBounds(xx,yy,super.getIcon().getIconWidth(), super.getIcon().getIconHeight());
		
		}

x and y are cursor offsets for its position on the card being dragged and xx and yy are the previous position, set in the mouse clicked event

how do I get whats underneath it?

Recommended Answers

All 4 Replies

GetDeepestComponent wont work since I dont have a child/parent relationship set up

And from what I understand about that Drag and drop interface is that information gets transfered from the dragged item to the drop target, thats kinda the opposite of what I want. I want to get the name and position of the blank image JLabel and use that information for determining the position of the card JLabel object.

I found a way of solving this, not the best way by far, but it will do for now..

I moved the card back to its previous spot then called the getComponent() and set its new location

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.