wood burning stoves*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "updating JPanel in a JFrame" Watch "updating JPanel in a JFrame" New topic
Author

updating JPanel in a JFrame

Adam Mellen
Greenhorn

Joined: Sep 19, 2003
Posts: 6
Hi,
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.setTitle(title);

F.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

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.setContentPane(this);
F.setJMenuBar(menuBar); // Add the menu bar to the frame

(later in code .. but still in the createWindow method)
drawingArea = new JPanel();
drawingArea.addMouseListener(this);
drawingArea.addMouseMotionListener(this);
drawingArea.setPreferredSize(new Dimension (Fwidth-20, Fheight-80));
drawingArea.setBackground(Color.white);
drawingArea.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(Color.black),
drawingArea.getBorder()));
F.getContentPane().add(drawingArea, BorderLayout.CENTER);
F.setVisible(true);
/* 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!");
drawingArea.repaint();
}
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
g.setColor(Color.black);
// 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)
{
g.drawLine(x1,y1,x2,y2);
}
if (type.compareTo("circle") == 0)
{
g.drawOval(x1,y1,x2,y2);
}
if (type.compareTo("rectangle") == 0)
{
g.drawRect(x1,y1,x2,y2);
}
} // paint method
Nathan Pruett
Bartender

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,


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

Joined: Sep 19, 2003
Posts: 6
Nathan,
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 java.sun.com 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!
Adam
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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: http://aspose.com/file-tools
 
subject: updating JPanel in a JFrame
 
Similar Threads
Writing Mouse Events
drawing arrows
enabling the scrollbar when components are dynamically added in jpanel
problem with JDialog
How to use menu event