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 need help using the paintComponent method 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 "need help using the paintComponent method" Watch "need help using the paintComponent method" New topic

need help using the paintComponent method

Todd Swift

Joined: Apr 11, 2006
Posts: 5
Hi, I'm reading the Head First Java book and am having problems using the example regarding the paintComponent method (pg. 364 chapter 12). I've made a subclass of JPanel but cannot seem to call the paintComponent method. I've pasted my code below; any tips would be appreciated!

package mydrawpaneltest;

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

public class MyJPanel extends JPanel {

/** Creates a new instance of MyJPanel */
public MyJPanel() {

public void paintComponent(Graphics g) {

g.fillRect(20, 50, 100, 100);



package mydrawpaneltest;

public class Main {

public Main() {

public static void main(String[] args) {

MyJPanel myJPan = new MyJPanel();
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
in the paintComponent(Graphics g)




You down with OOP? Yeah you know me!
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
if that is all of your code, the panel needs to be in a frame/contentPane
and for the frmae to be visible.

try this for your Main()

Todd Swift

Joined: Apr 11, 2006
Posts: 5
Thanks for your help; putting the panel in the frame worked! Thanks to all for your help.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: need help using the paintComponent method
Similar Threads
Printing a JPanel
Applet Login via database
problem with paint methods
KeyListener() is sometimes deaf
JTable problem: not visible