This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Adding drawpanels to a frame Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Adding drawpanels to a frame" Watch "Adding drawpanels to a frame" New topic

Adding drawpanels to a frame

Nathan Przybylo

Joined: Oct 19, 2006
Posts: 2
I'm using an overridden JPanel to act as a drawPanel for me to put bar charts in. The problem is, I can only see the graphics in the panel when I add it to the CENTER of the frame. Anywhere else, and it just won't appear on the frame. How can I set it up so that the drawpanel can be added to the SOUTH region?

Below is an example from Head First Java, if you switch the layouts of the button and the drawpanel, you will notice that the drawpanel will not appear.


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class SimpleGui3C implements ActionListener {

JFrame frame;

public static void main(String[] args) {
SimpleGui3C gui = new SimpleGui3C();

public void go() {
frame = new JFrame();

JButton button = new JButton("Change colors");

MyDrawPanel drawPanel = new MyDrawPanel();


public void actionPerformed(ActionEvent event) {

The MyDrawPanel class simply overrides JPanel's paintComponent with a simple rectangle.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
sounds like your overridded JPanel has no size.

when added to SOUTH, it is sized to it's preferredSize (0,0), so doesn't appear.
when added to CENTER, borderlayout gives center all the remaining area of the
size of the frame (after NORTH,SOUTH,EAST,WEST is allocated), regardless of the
preferredSize of what's in CENTER.

test by including this in the constructor of your drawPanel
setPreferredSize(new Dimension(100,100));
now add it to SOUTH
jQuery in Action, 2nd edition
subject: Adding drawpanels to a frame
Similar Threads
problem with simple swing GUI program
Event Problems: Need Help
not able to get the coloured oval
need help with my code
Understanding event handling