This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach 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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Adding drawpanels to a frame" Watch "Adding drawpanels to a frame" New topic
Author

Adding drawpanels to a frame

Nathan Przybylo
Greenhorn

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.

Thanks!
-Nathan

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();
gui.go();
}

public void go() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JButton button = new JButton("Change colors");
button.addActionListener(this);

MyDrawPanel drawPanel = new MyDrawPanel();

frame.getContentPane().add(BorderLayout.SOUTH,button);
frame.getContentPane().add(BorderLayout.CENTER,drawPanel);
frame.setSize(300,300);
frame.setVisible(true);
}

public void actionPerformed(ActionEvent event) {
frame.repaint();
}
}

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: http://aspose.com/file-tools
 
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