• Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding drawpanels to a frame  RSS feed

Nathan Przybylo
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!