Problems with JPanel

N

Netlopa

Hello,

I'm creating a project for the university and I have currently a problem.
I created a JPanel that contains within it an array of (x * y) JLabel (which
by the method setIcon are just pictures and have nothing to text).
Now I want to create pieces, which are expected to move around the board,
but I find myself with a problem of "top".
In practice, the pieces are hidden from the board itself (JLabel array) and
then you can not see.
Then, I could not find any method that allows to switch JPanel (or just the
array of JLabel) into the background so as not to hide the pieces (which
should be set as in the first stage).

Someone can enlighten me;)

Thank you,
netlopa
 
I

Ian Shef

Netlopa said:
Hello,

I'm creating a project for the university and I have currently a
problem. I created a JPanel that contains within it an array of (x * y)
JLabel (which by the method setIcon are just pictures and have nothing
to text). Now I want to create pieces, which are expected to move around
the board, but I find myself with a problem of "top".
In practice, the pieces are hidden from the board itself (JLabel array)
and then you can not see.
Then, I could not find any method that allows to switch JPanel (or just
the array of JLabel) into the background so as not to hide the pieces
(which should be set as in the first stage).
<snip>

I think that by "top" you mean what is usually called stacking order or Z-
order.
You provided no code, but I am going to guess at your intent here. I am
guessing that you used add(...) to put your JLabel_s on your JPanel. Then
you used add(...) to put your pieces on your JPanel. Note the following
from the Javadoc for Container:

"Components added to a container are tracked in a list. The order of the
list will define the components' front-to-back stacking order within the
container. If no index is specified when adding a component to a container,
it will be added to the end of the list (and hence to the bottom of the
stacking order)."

When you throw papers on your desk, the last ones thrown onto the desktop
land on top. By default, JPanel does not work the way. The last things
add_ed go to the bottom.

The solution is to either:
- Add the pieces before the JLabels.
- Use one of the overloaded versions of add(...) that have an index
parameter, and use an index of zero.
- Use setComponentZOrder(...) to change the stacking order.
- Use two JPanel_s, adding pieces to one and JLabel_s to the other. Now
you don't have to worry about what order you add the pieces and the
JLabel_s, just what order the two JPanel_s got added to their Container.

Also note that "If a component has been added to a container that has been
displayed, validate must be called on that container to display the new
component."

Good luck!
 
R

Roedy Green

I'm creating a project for the university and I have currently a problem.
I created a JPanel that contains within it an array of (x * y) JLabel (which
by the method setIcon are just pictures and have nothing to text).
Now I want to create pieces, which are expected to move around the board,
but I find myself with a problem of "top".
In practice, the pieces are hidden from the board itself (JLabel array) and
then you can not see.
Then, I could not find any method that allows to switch JPanel (or just the
array of JLabel) into the background so as not to hide the pieces (which
should be set as in the first stage).

You might find this easier to handle with drawImage drawString etc.
You have perfect control of positioning and painting order. It is
also quicker than using arrays of Components.

see http://mindprod.com/jgloss/jpanel.html


--
Roedy Green Canadian Mind Products
http://mindprod.com

"Everybody’s worried about stopping terrorism. Well, there’s a really easy way: stop participating in it."
~ Noam Chomsky
 
J

John B. Matthews

"Netlopa" <[email protected]> said:
I'm creating a project for the university and I have currently a
problem. I created a JPanel that contains within it an array of (x *
y) JLabel (which by the method setIcon are just pictures and have
nothing to text). Now I want to create pieces, which are expected to
move around the board, but I find myself with a problem of "top". In
practice, the pieces are hidden from the board itself (JLabel array)
and then you can not see.
Then, I could not find any method that allows to switch JPanel (or
just the array of JLabel) into the background so as not to hide the
pieces (which should be set as in the first stage).

I simulated something like this show/hide behavior of game pieces using
JToggleButton, as shown here:

<http://sites.google.com/site/drjohnbmatthews/buttons>
 
N

Netlopa

Thank you for all your answer.

Finally, I've created a JLabel with a transparent image with this code

private JLabel getLblTransparent() {
if (lblTransparent== null) {
lblTransparent = new JLabel();
lblTransparent.setIcon(new ImageIcon("transparent.gif"));
}
return lblTransparent;
}

Caller code:

jPanel0.add(getLblTransparent(), new Constraints(new Leading(0, 14, 14),
new Leading(0, 14, 14)));
[..omissis..]
// Array of x*y cells

And next I have the code to show dynamically the 'moving pieces' constructed
to lblTransparent:

lblTransparent.add(lblPiece);
lblPiece.setBounds(
character.get(i).getPedina().getX()*this.xIcona+((xIcona-icona.getIconWidth())/2)
,
character.get(i).getPedina().getY()*this.yIcona+((yIcona-icona.getIconHeight())/2)
, icona.getIconWidth()
, icona.getIconHeight());

Thanks to all and see you soon!

Netlopa
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,269
Latest member
vinaykumar_nevatia23

Latest Threads

Top