views:

224

answers:

4

Hi everyone,

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?

A: 

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.

pstanton
Note: add(...) invokes addImpl(...) which is implemented by JLayeredPane.
camickr
ah.but moveToFront/moveToBack and setLayer is the recommended approach if you read the docs:"This sequence (defined by java.awt.Container) is the reverse of the layer numbering sequence. Usually though, you will use moveToFront, moveToBack, and setLayer."
pstanton
A: 

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.

camickr
I am aware of the difference between the two methods.As you correctly stated, in the first case the component is added at the end of the container and in the second case at the specified position in the container.But the problem is that there's nothing else on the drag layer except from the chesspiece. That's exactly the problem. Therefore, it shouldn't matter which method I choose to use. And moreover, how do you explain the fact that sometimes (while dragging) the chesspiece seems to appear behind the chessboard?Here's a link to the code:http://nopaste.com/p/akZeOG7Bb
Konos5
A: 

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...

Konos5
A: 

in fact u must use da glasspane. before u start to translate de label u must glasspane.add(label); then label.setLocation(x,y); inside the MouseDragged event, but the listener must be MouseMotionListener and not MouseListener.

PD: sorry for ma bad english! salut!

Cristian