Add a JPanel to a JPanel...

A

Andreas Beresko

Hi,

I got followning problem: i have visible JPanel on the screen. When i click
on it, a contet menu appears, where I can coose "add another panel".
This calls a method paintNewPanel() and should add a new visible panel on my
panel. Code:

public void paintNewPanel(JPanel drawArea){ //is the JPanel which captures
the mouseclick an which should contain the new panel

JPanel panel = new JPanel();
panel.setBackground(Color.gray);
drawArea.add(panel);
panel.setBounds(0,0,100,100);

}

Problem: the new Panel isn't visible. I tried to call drawArea.repaint,
invalidate ... but nothing happens. Where's my fault?

best regarda Andreas
 
T

Tor Iver Wilhelmsen

Andreas Beresko said:
Problem: the new Panel isn't visible. I tried to call drawArea.repaint,
invalidate ... but nothing happens. Where's my fault?

1) Are you sure drawArea has a null layout? Swing/AWT will laugh
heartlessly at your setBounds() otherwise.

2) Make sure the JPanel is opaque. I think they are by default though.
 
A

Andreas Beresko

Thanks for answer, but...
Problem: the new Panel isn't visible. I tried to call drawArea.repaint,
invalidate ... but nothing happens. Where's my fault?

1) Are you sure drawArea has a null layout? Swing/AWT will laugh
heartlessly at your setBounds() otherwise.

..... I'm sure it's null layout...

2) Make sure the JPanel is opaque. I think they are by default though.

.... when I set the first Panel to nonopaque I see my secon Panel I lay on
it, BUT: i want to add some
Mouse listeners to my second Panel and so it has to be in front of my first
(background panel)...

any other suggestion?
 
T

Tor Iver Wilhelmsen

Andreas Beresko said:
... when I set the first Panel to nonopaque I see my secon Panel I lay on
it,

This seems to indicate that your "first" and "second" panels are in
the same container, not the second inside the first. Perhaps also that
what you think of as the background panel gets added after the "second
panel".
BUT: i want to add some Mouse listeners to my second Panel and so it
has to be in front of my first (background panel)...

This should be automatic as long as

You can try rearranging them by adjusting z-order.
 

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

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,685
Members
48,796
Latest member
Greg L.

Latest Threads

Top