• 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

Problem in reloading graphics object.

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am very new to swing and java awt. I am trying an application where I have 2 panels in main container. First panel (p1)contains some data and a button , upon click of button, Second panel (p2) has to draw a graph based on the content of first panel.
To draw the graph I am creating another class called secondPanel and passing p2 in the constructor, to get the graphics object of p2 to draw the graph.
The problem I am getting is when I change the data and redo the graph it retains the old graph. As I understand it is because of the graphics object that I am getting from p2.
before passing object p2 to the constructor of SecondPanel, I am calling removeAll() and validate() on p2, however my graphics object is not recreated.
Really appreciate if someone can help me here. Here is the code snippet of SecondPanel.


In the main container here is my code of reloading p2
How can I reload the graphics. while using super.paintComponent(g), I am getting null pointer exception and I don't know whether graphics object obtained by super will fit my requirement.
Thanks in advance!!!.

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm not sure what are you trying to achive.
Let me summirize it:
  • SecondPanel extends JPanel
  • It's only member is JPanel
  • When SecondPanel is created, you add the ref from a foo JPanel
  • Afterwards you add the SecondPanel to foo

  • A bit complicated, don't you think? ;)
    You should use a CardLayout (p1 vs. p2).

    Anyhow ... usually an invalidate() and repaint() on the component should do the trick.


    Regards,
    Rok
     
    Mumtaz Khan
    Ranch Hand
    Posts: 53
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Yeah it was a bit complicated . So I changed my approach instead of using another class, I am using an inner class that extends JPanel and it works. The code posted above was not working even with invalidate and repaint may be because I was not calling super.paintComponent(g); .I think this needs to be called for repainting graphics object.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic