Results 1 to 4 of 4
  1. #1
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    257
    Rep Power
    7

    Default Nested JPanel not rendering as expected

    Hello everybody!

    I am a JFrame with two nested JPanels, one of these also has a nested JPanel.

    This is the expected outcome:

    Nested JPanel not rendering as expected-prob.png

    This is the outcome:

    Nested JPanel not rendering as expected-lol.png

    This is the code:

    Java Code:
    public class DisplayFrame extends JFrame{
    
    	private DisplayCanvas canvas;	
    	private ControlContainer controlContainer;
    
    	public DisplayFrame() {
    
    		//simple inheritance. 
    		super(title);
    		setSize(new Dimension(1200, 800));
    		setResizable(false);
    		setLocationRelativeTo(null);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setVisible(true);
    
    		addComponents(this.getContentPane());
    
    	} 
    
    	public void addComponents(Container container){
    
    		canvas = new DisplayCanvas();
    		controlContainer = new ControlContainer();
    
    		container.add(canvas);
    		container.add(controlContainer);
    
    		System.out.println("canvas width: " + canvas.getWidth());
    		System.out.println("controlContainer width: " + controlContainer.getWidth());
    
    
    	}
    
    	public void render(){
    
    		canvas.repaint();
    
    	}
    
    }

    Java Code:
    public class DisplayCanvas extends JPanel{
    
    	public DisplayCanvas() {
    
    		this.setSize(new Dimension(800, 800));
    		this.setBackground(Color.WHITE);
    		this.setVisible(true);
    
    	}
    
    	@Override
    	protected void paintComponent(Graphics g){
    
    		super.paintComponent(g);
    		for(int i = 0; i < 10; i++){
    			g.fillRect(i*50, i*20, 100, 100);
    		}
    
    	}
    
    }

    Java Code:
    public class ControlContainer extends JPanel{
    
    	private ControlPanel controlPanel;
    
    	public ControlContainer() {
    
    			
    		setSize(new Dimension(400,800));
    		setBackground(Color.GRAY);
    		addControlElements();
    		setVisible(true);
    
    	}
    
    
    	public void addControlElements(){
    
    		controlPanel = new ControlPanel();
    		this.add(controlPanel);
    
    
    	}
    
    
    }
    Java Code:
    public class ControlPanel extends JPanel{
    
    	public ControlPanel() {
    
    		setSize(new Dimension(300, 600));
    		setBackground(Color.GREEN);
    		setVisible(true);
    		System.out.println("nested (green) width: " + getWidth());
    
    	}
    
    }
    The output of the print statements:

    Java Code:
    nested (green) width: 300
    canvas width: 800
    controlContainer width: 400
    Ive tried using GridBagLayout but it kept showing both JPanels as tiny squares in the middle of the JFrame.

    Thanks.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Nested JPanel not rendering as expected

    Clearly you can't run that code. The variable title isn't even defined. And there is no main entry point.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Nested JPanel not rendering as expected

    Moved from New to Java to AWT/Swing.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Nested JPanel not rendering as expected

    1. Don't use setSize(...). Learn about layout managers: Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    Note that top level windows like JFRame (or more strictly speaking, the contentPane of a top level window) have a BorderLayout by default; components added without a constraint are added to the BorderLayout.CENTER position; and the BorderLayout manages only the last component added with each constraint. A JPanel has a default FlowLayout.

    2. Components other than top level windows are visible by default; no need to setVisible(true) for a JPanel subclass.

    3. Don't subclass unless you need custom behavior. Of your four classes, only DisplayCanvas meets that criterion.

    I suggest you go through the entire trail in the tutorial I linked, doing the exercises.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 0
    Last Post: 02-17-2013, 09:21 PM
  2. Replies: 1
    Last Post: 11-30-2011, 08:24 AM
  3. Rendering graphics on JPanel
    By wired-in=p in forum New To Java
    Replies: 4
    Last Post: 10-06-2011, 06:08 AM
  4. Replies: 20
    Last Post: 03-22-2011, 04:02 AM
  5. Replies: 11
    Last Post: 03-09-2011, 08:18 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •