Reputation: 8042
I am currently working on a drag n drop application and I would really like to know what's happening inside the JLayeredPane and I get a particular program behaviour...
Here's the deal:
I have a chessboard placed on the DEFAULT_LAYER.
I also have a chessPiece which I'd like to be added to the DRAG_LAYER when I move it.
But I have a fetish...
I want to use this line
layeredPane.add(chessPiece, JLayeredPane.DRAG_LAYER);
only under the mouseDragged event.
So, when I do this, my chessPiece disappears while moving my mouse and gets hidden behind the chessboard (?!?)
When I change the above line into this:
layeredPane.add(chessPiece, JLayeredPane.DRAG_LAYER,0);
everything becomes normal again.
Why is that happening?
Upvotes: 1
Views: 1170
Reputation: 11
You must use GlassPane. Before you start to translate the label you must use GlassPane.Add(label) and label.setLocation(x,y) inside the MouseDragged event. The listener must be MouseMotionListener and not the plain MouseListener.
Upvotes: 1
Reputation: 8042
I have also used the above diagnostic and here's what happens:
int number = layeredPane.getLayer(chessPiece);
int number3 = layeredPane.getComponentCountInLayer(JLayeredPane.DRAG_LAYER);
While dragging, the first one always returns the number 400 which is the notation for the drag layer. BUT the other one returns a 1 at first but then it all become zeros as we drag...
This means that at least for an instance of time, the chesspiece actually GOT into the drag layer and then fell off. But that's what happens if you ask the pane...
Does this help at all? Also, would you mind giving me a link of the jlayeredpane source code?
I'm looking for JLayeredPane.java but had no luck till now...
Thanx in advance...
Upvotes: 0
Reputation: 324118
The question is what is the difference between the two methods? Well if you read the API you will find the difference. In the first case the component is added at the "end" of the container. In the second case the component is added at the "specified position" in the container.
Since there is only one component added to the DragLayer, theorectically it shouldn't make a difference which add method is used. However, because you also have a component on the DefaultLayer, it may may a difference. The only way to know for sure is to look at the source code.
However as has already been suggested in your other posting. This code should not be done in the mouseDragged() event. As multiple drag events are generated, it does not make sense to continually "add" the chess piece to the drag layout. All you need to do is change the location of the chess piece as the mouse is moved. That is why the mousePressed() event is used to add the chess piece to the drag layer.
Upvotes: 0
Reputation: 36640
I use layeredPane.moveToFront(component)
instead of relying on the index
parameter.
Note that the add(Component comp, Object constraints, int index)
is implemented by java.awt.Container
, not javax.swing.JLayeredPane
.
Upvotes: 1