Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Using setBounds with jLayeredPane

807605Jul 23 2007 — edited Jul 27 2007
I am trying to use setBounds with a jLayeredPane. For some reason the setBounds constraint does not work unless I add another component to the jFrame AFTER I add the jLayeredPane to my jFrame. For example, if my only component on my frame is a jLayeredPane, and I add it as follows:
this.add(layeredPane);
it does not work. The bounds fill the entire jFrame.

If I have other components before, such as:
this.add(jl);
this.add(layeredPane); 
It still does not work. The layeredPane fills the entire JFrame and I cannot see the jl component.

Yet if I have:
this.add(layeredPane);
this.add(jl);
Then it works. The bounds works on the jLayeredPane.

Can someone explain to me why a component needs to be added AFTER I add the jLayeredPane for the bounds to work?

Attached is some code which I simplified which displays the problem:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class Main extends JFrame
{
	JLayeredPane layeredPane;
	Component dragComponent;
	int xAdjustment;
	int yAdjustment;

 
	public Main()
	{
                
                       
                JLabel jl = new JLabel("Hello");

		layeredPane = new JLayeredPane();
		layeredPane.setLayout( null );
                //layeredPane.setPreferredSize(new Dimension(300,300));
                //layeredPane.setMaximumSize(new Dimension(400,400));
                layeredPane.setBorder(BorderFactory.createLineBorder(Color.GREEN));
                layeredPane.setBounds(50,50,600,500);

                /** this is the line explained in my question **/
                this.add(layeredPane);                

	}
        
 
	public static void main(String[] args)
	{
		JFrame frame = new Main();
		frame.setDefaultCloseOperation( DISPOSE_ON_CLOSE );
		frame.setSize(600, 600);
		frame.setLocationRelativeTo( null );
		frame.setVisible(true);
	 }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 24 2007
Added on Jul 23 2007
3 comments
888 views