Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to connect a JPanel created by JFreeChart to JFrame

 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear boss,
This question may be trivial

http://www.java2s.com/Code/Java/Chart/JFreeChartPieChartDemo1.htmSorry about the naming, Panel == Canvas here. No time to correct.
Thanks
 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should have corrected that naming conflict because you would not be able to compile the code otherwise, and you are doubtless using copy‑and‑paste to post your code.
You should be adding that panel in your initComponents method, and it must be before your setVisible(true) call if you want to see it. Or you have to call validate() or revalidate().

Moving discussion to our GUIs forum.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your createDemoPanel() method returns JPanel, so you add it to your frame like you would with any other:


Note that methods in your class shouldn't be static and there is no need for that class to extends ApplicationFrame.
 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kemal Sokolovic wrote:Your createDemoPanel() method returns JPanel, so you add it to your frame like you would with any other:


Note that methods in your class shouldn't be static and there is no need for that class to extends ApplicationFrame.


Thanks Sokolovic, it works, want to give you 2 plus'es.
Update: it only works once :S
When I change the code, it works, but it stops working afterwards...
It looks very wierd, it comes on and off.






Jack
 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think invalidate() might not quite be the correct method to call.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by:
... but it stops working afterwards...


You should add your panel to frame before you invoke setVisible(true). As Mr Campbell pointed out, invalidate() shouldn't be there.
 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, everybody, it works like a charm!
 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome

And it’s Mr Ritchie not Mr Campbell
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:And it’s Mr Ritchie not Mr Campbell


If you found yourself offended, that certainly wasn't my intention.
 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I’d been offended, I would not have used the

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic