Hi,
I've searched the Java API, these forums and the internet for the solution to this problem and I can't seem to find anything so all help would be appreciated.
I'm using a JLayeredPane to display a chess board with dragable chess pieces set as JLabels. I create my JLabels without a reference (for implementation reasons) and place then on the board at the appropriate position i.e. in psuedo- code
new JLabel(ChessImage, position x).
To access the images I merely click on the JLayeredPane and my mousePressed() method locates the label by using the getComponent() method. This returns the first component at the closest depth/layer which is great as two pieces can never occupy a square at the same time. I can then drag the selected component around and place it where I want to.
The problem occurs when I come to drop the image. Obviously the idea would be to find the component underneath the dragged piece, remove it and placed the dragged piece in its place. In my mouseReleased() method I locate the componet to be replaced using the getComponent() method again and then use the remove() method to remove it from the JLayeredPane. The dragged componet is then set in its place.
However, sometimes things seem to get a little confused and the remove() method removes both the dragged component and the one it is trying to replace. This therefore is a problem
My first thought as to how to overcome this was to set the dragged componet to the JLayeredPane drag layer. When it came to dropping the piece on its target I would locate the piece to be replaced by using the getComponent() method but specifying the layer that I wish to retrieve the component from. The dragged piece would therefore be safe as it is in another layer and there is no chance that it would be returned from the getComponent() method as it is in another layer. However, I can't seem to find a method which will return components at a particular depth/layer. Do such methods exist? The getComponent() method only seems to return components highest up in the layer.
I maybe wrong about the remove() method but the results from my program have lead me to believe this is the case. Sometimes when moving pieces around they just suddenly disappear so I'm assuming that the remove() method is taking them away.
Again all help is appreciated as I'm a little short on time. Hope the explanation of the problem was clear.