• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

updating JPanel in a JFrame

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Adam Mellen
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic