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 updating JPanel in a JFrame Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "updating JPanel in a JFrame" Watch "updating JPanel in a JFrame" New topic

updating JPanel in a JFrame

Adam Mellen

Joined: Sep 19, 2003
Posts: 6
I just found this site the other day, and it looks like a great resource! Anyway, I am creating a (simple) Java drawing program and have as my top-level window a JFrame. Inside the JFrame is a JPanel, upon which the drawings (lines, circles, rectangles) will appear. However, when I go to draw items on my JPanel, it looks like they are appearing on my JFrame (underneath the JPanel), and I can't figure out how to get the drawings to appear in the JPanel. I've posted code below, in case that will help you understand what it is I am trying to do.... Thanks in advance for any help/suggestions you may have...
--- code ---
/* Global Variables */
JFrame F;
JPanel drawingArea;
(later in code)
/* parts of the createWindow method
F = new JFrame();

F.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {

Fwidth = Fheight = 500;

F.setBounds(100,200,Fwidth,Fheight); //set the position and size of the frame

menuBar = new JMenuBar();// Create a menu bar object
F.setJMenuBar(menuBar); // Add the menu bar to the frame

(later in code .. but still in the createWindow method)
drawingArea = new JPanel();
drawingArea.setPreferredSize(new Dimension (Fwidth-20, Fheight-80));
F.getContentPane().add(drawingArea, BorderLayout.CENTER);
/* end of createWindow method */
public void drawingAreaChanged()
/* this method gets called whenever the user selects the option to start
* drawing
System.out.println("Drawing Area Changed!");
public void paintComponent (Graphics g)
String type;
int x1, y1, x2, y2;
int count;
Integer tempi;
Vector imageData = myModel.getImageData();

super.paintComponent(g); // repaints the background of JFrame
// type is set in another part of the code
// All of these draw commands appear in the JFrame rather than the
// JPanel
if (type.compareTo("line") == 0)
if (type.compareTo("circle") == 0)
if (type.compareTo("rectangle") == 0)
} // paint method
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

What class is this line in?

Basically what this line does is set the current object as the contents (i.e. background and where components get added) of the JFrame. So if the later paintComponent() code is also in the same class as the line above, then the program is doing exactly what you told it to (which might be completely different than what you *want* it to do. )

Welcome to the JavaRanch,

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Adam Mellen

Joined: Sep 19, 2003
Posts: 6
First of all, Thanks for the advice!
Secondly, The line: F.setContentPane(this);
is in the same class as the other code that was posted, and what you said makes sense. After doing some searching about setContentPane on the site, I realized that what I need to do is something like F.setContentPane(drawingArea); (at least that is what I think I need to do)... However, when I tried this is my code, the drawingArea JPanel showed up, but now the paintComponent method never gets called (I am calling repaint() where needed). Any suggestions as to why the paintComponent method isn't recieving the event that repaint() should be sending to it?
thanks in advance for any advice you may have!
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

I'm having a hard time following this, because it's not a complete program. But the problem may be visible in the following line of code:

drawingArea is the panel on which you want the images to appear, yes? Well, the way to draw onto a JPanel is to subclass JPanel and override the paintComponent() method of JPanel. If you're creating drawingArea as an actual concrete JPanel then you're clearly not doing that. Maybe the paintComponent you're showing us is in a subclass of JFrame, instead of in the JPanel?

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: updating JPanel in a JFrame
jQuery in Action, 3rd edition