954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Resizing JPanel in JPanel in JFrame

public class TEST extends JFrame{
    ... blah, blah.....
	
    private void launch(){
	setBounds(200, 200, 100, 100);
	setDefaultCloseOperation(EXIT_ON_CLOSE);
	
	JPanel greenPanel = new JPanel();
	greenPanel.setSize(70, 70); //<---- xNot resize the greenPanel
	greenPanel.setBackground(Color.green);
	
	JPanel yellowPanel = new JPanel();
	yellowPanel.setSize(50, 50); //<-- xNot resize yellowPanel in greenPanel
	yellowPanel.setBackground(Color.yellow);
	greenPanel.add(yellowPanel);
	
	JPanel bluePanel = new JPanel();
	bluePanel.setBackground(Color.blue);
	bluePanel.add(greenPanel);
	
	add(bluePanel);
	
	setVisible(true);
    }
}


So, I have a yellow JPanel inside a green JPanel inside a blue JPanel.. For some reason, none of the JPanels resize, only the JFrame...

Here's a picture: http://img225.imageshack.us/img225/5460/jpanelbashsn7.png

Any ideas?

TheWhite
Junior Poster
174 posts since May 2008
Reputation Points: 72
Solved Threads: 6
 

The default FlowLayout gives you very little control over component sizing/resizing behavior. Try a different layout manager to achieve the behavior that you want:
http://java.sun.com/docs/books/tutorial/uiswing/layout/index.html

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

First of all, I want to say thanks! I had hard time doing JPanel over JPanel and in such colourful box.

change your setSize into setPreferredSize(new Dimension(int length, int width))

Azeerazly
Newbie Poster
1 post since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You