Accessing JLable within JLayerPane

Hi, I am having problem dealing with JLabel object in JLayerPane so that i can restrict its movement. I have try to use board.getComponentAt() but it keep giving me error. Can anyone give me some advise?

The code below is a part of 8x8 board.

layeredPane =new JLayeredPane();

getContentPane().add(layeredPane);

board =new JPanel();

layeredPane.add(board, JLayeredPane.DEFAULT_LAYER);

board.setLayout(new GridLayout(0, 8) );

box =new JPanel(new BorderLayout() );

board.add( box, BorderLayout.CENTER );

box.setBackground(Color.lightGray);

for (int i = 0; i < 64; i++)

{

//adding black pieces

if (i < 8)

{

String fileName = i +".gif";

JLabel blackPawn =new JLabel(new ImageIcon( fileName ) );

JPanel panel = (JPanel)board.getComponent( i );

panel.add( blackPawn );

blackPawn.setName("blackPawn");

}

Sorry if it doesnt make sense. As I am very new using the forum.

[1498 byte] By [Dino2526a] at [2007-11-27 10:34:29]
# 1

Your concept of getComponentAt is incorrect. Please read the API before posting here. You'll see all of this. You can't just give it an int variable and expect it to know what component is there. It requires an x and y coordinate position relative to 0,0 of the calling container. I doubt that this is the method that you want to use.

the API for getComponentAt can be found here:

http://java.sun.com/javase/6/docs/api/java/awt/Container.html#getComponentAt(int,%20int)

petes1234a at 2007-7-28 18:29:11 > top of Java-index,Desktop,Core GUI APIs...
# 2

what you probably need is an array of JPanels (or whatever you need to use here), and then you can use an index variable to get the correct object.

petes1234a at 2007-7-28 18:29:11 > top of Java-index,Desktop,Core GUI APIs...
# 3

> Sorry if it doesnt make sense. As I am very new using the forum.

Being new to the forum has nothing to do with asking a question that makes sense.

Your question says you are trying to use getComponentAt(...).

Your code shows you using the getComponent(...) method.

You say you get an error but don't state what the error is.

You copied the code from an example that compiles and executes without any problem. We don't know why you are making changes or what you are attempting to do. Stating that you get an error gives us no information whatsoever.

How does the code you are posting relate to restricting the movement of the label. The code you posted shows how the chess board is initialize and has absolutely nothing to do with dragging or moving the label around the board.

So how do you expect us to help?

You have already been given suggestions in two other postings on this topic which you apparently have ignored.

camickra at 2007-7-28 18:29:11 > top of Java-index,Desktop,Core GUI APIs...