File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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
I agree. Here's the link:
subject: Adding drawpanels to a frame
It's not a secret anymore!