• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Getting a JFreeChart inside a specific JPanel

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi java people! I've really been having a hard time getting my head around this. First of all, I have this class which builds a GUI, which is pretty much exactly the layout I want.



Okay, now run it. You see that large JPanel on the left. I want to get a chart of some description to take up that space. I've been using JFreeChart, and I'm more or less used to making them at this point, but they always open in their own screen when I try and combine them with the above class. Since I haven't really decided what type of chart needs to go in there yet, here is a sample one from the JFreeChart people themselves, which i was using to get some grasp of how to manipulate charts inside a panel:



So, given this example, how would I get the charts from the second class at the location I want in the first class?



 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since it is a JComponent, just add the ChartPanel you have created on line 64 of InternalFrameDemo as a child to the JPanel !
 
Terry Flint
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, this is probably a stupid thing to ask, but I'm fairly new. When you say "child" does that mean the same thing as it does when talking about inheritance?
 
Richard Tookey
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Terry Flint wrote:Okay, this is probably a stupid thing to ask, but I'm fairly new. When you say "child" does that mean the same thing as it does when talking about inheritance?



No !!! You have a JPanel so just use it's add() method to add the ChartPanel to it. Frequently one can just use the ChartPanel instad of the JPanel so where in your code you add the JPanel your may be able to just add the ChartPanel.
 
Terry Flint
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So this portion of the Statistics class should look something like this?

 
Richard Tookey
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Terry Flint wrote:So this portion of the Statistics class should look something like this?



Try it ! Or use the chartPanel in the addComponent() then you don't need the graphingPanel .
 
Terry Flint
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, I actually thought this would be easy to figure out, since it sounds like it should be simple. I have no idea how to get this line to be read by the statistics class.



My usual instinct would be to create a getter for chartPanel , but since it is already inside the createFrame1 method, that won't work. I also tried making the createFrame1 method public and returning chartPanel instead of the frame itself, but that just causes a compile error. I'm sure I'm missing something obvious, but I just can't see it.
 
Richard Tookey
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't try to cobble together the two disjoint bits of code. Extract the few lines of code that create the ChartPanel from the JFreeChart example and insert them into the method that creates the graphing panel. You can then discard the rest of the JFreeChart example code. You will need to write your own code to populate the JFreeChart model but for test purposes you can just copy the example createDataset() method into your class. Later you can modify the createDataset() method so it populates the chart with your own data.
 
Terry Flint
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I actually just managed to get what I wanted, but I did cobble the code together a bit. I will try and do it the way you just recommended though. For the sake of those that may be having problems with this in the future, I'll post the solution when I manage to do it the correct way. For now, I'm considering this problem to be resolved.
 
Terry Flint
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe this is a more acceptable version than what I had before.

 
reply
    Bookmark Topic Watch Topic
  • New Topic