AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Problem when add a JPanel to a JLayeredPane

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java GUI Toolkits
View previous topic :: View next topic  
Author Message
cudjoe
Guest





PostPosted: Sat Sep 20, 2003 9:36 am    Post subject: Problem when add a JPanel to a JLayeredPane Reply with quote



Hi,

I'm trying to add a JPanel to a JLayeredPane, but it unfortunately
never appears...
However when I add a JLabel to this layered pane, it works fine...

Could anyone tell me which properties shall I set to this JPanel, so
that it appears?

Thank you very much,

Greetings from France !

Mathieu.

Here is the problem illustration :

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

public class TestLayerPane extends JPanel {

public TestLayerPane() {
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.setPreferredSize(new Dimension(200, 200));

layeredPane.add( getLabel( Color.red, 30), new Integer(0));

//Works fine :
//layeredPane.add( getLabel( Color.yellow, 20), new Integer(1));

//Doesn't work :
JPanel subPanel = new JPanel();
subPanel.add( getLabel( Color.yellow, 20) );
layeredPane.add( subPanel, new Integer(1));

add( layeredPane );
}

public JLabel getLabel( Color color , int position ){
JLabel label = new JLabel();
label.setOpaque(true);
label.setBounds( position, position, 5*position, 5*position);
label.setBackground( color );
return label;
}

public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setContentPane(new TestLayerPane());
frame.pack();
frame.setVisible(true);
}
}
Back to top
Shannon Hickey - Swing Te
Guest





PostPosted: Sun Sep 21, 2003 4:21 am    Post subject: Re: Problem when add a JPanel to a JLayeredPane Reply with quote



Hi Mathieu,

[email]cudjoe (AT) europe (DOT) com[/email] (cudjoe) wrote in message news:<b1177e67.0309200136.1828f711 (AT) posting (DOT) google.com>...
Quote:
Hi,

I'm trying to add a JPanel to a JLayeredPane, but it unfortunately
never appears...
However when I add a JLabel to this layered pane, it works fine...

Could anyone tell me which properties shall I set to this JPanel, so
that it appears?

The reason that the label works when you add it directly is that
you're setting its bounds with respect to its parent (the
JLayeredPane).

When you add the JLabel to the panel, you're setting the label's
bounds with respect to the JPanel. You'll then also need to call
setBounds on the JPanel to indicate where it should be positioned
within the JLayeredPane.

Hope that helps Smile
Shannon
--
Shannon Hickey
[email]shannon1 (AT) swingteam (DOT) com[/email]
Swing Team http://TheSwingConnection.com http://TheJavaTutorial.com
Java Software, a division of Sun Microsystems, Inc.

Quote:

Thank you very much,

Greetings from France !

Mathieu.

Here is the problem illustration :

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

public class TestLayerPane extends JPanel {

public TestLayerPane() {
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.setPreferredSize(new Dimension(200, 200));

layeredPane.add( getLabel( Color.red, 30), new Integer(0));

//Works fine :
//layeredPane.add( getLabel( Color.yellow, 20), new Integer(1));

//Doesn't work :
JPanel subPanel = new JPanel();
subPanel.add( getLabel( Color.yellow, 20) );
layeredPane.add( subPanel, new Integer(1));

add( layeredPane );
}

public JLabel getLabel( Color color , int position ){
JLabel label = new JLabel();
label.setOpaque(true);
label.setBounds( position, position, 5*position, 5*position);
label.setBackground( color );
return label;
}

public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setContentPane(new TestLayerPane());
frame.pack();
frame.setVisible(true);
}
}

Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java GUI Toolkits All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.