• 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

adding graphics to panels

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have a class that generates pie charts using paint(Graphics) method. It works because i can add it to a JFrame in a main method and run it. The problem lies when i try and add an instance of it to a JPanel in a different class. nothing is rendered and no error is returned. any ideas?
thanks,
z
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The default layout manager for a JFrame is BorderLayout. Adding a graphic component to the center section will result in the component filling the available space. The default layout manager for a JPanel is FlowLayout which will ask the graphic component for it's preferred size (something that you could do, too). If the graphic component doesn't have any way of returning a Dimension that it considers desirable then the FlowLayout may not have much to work with. The default size for a JPanel is (10, 10). One thing you could do is to set the layout manager in the JPanel to a layout that ignores the preferred size of the graphic component and will expand it to fill the available space. Possibilities include the center section of a BorderLayout, GridLayout initialized to (0,1), and GridBagLayout with both weight constraints set to non–zero and the fill constraint set to GridBagConstraints.BOTH.
 
zack chancery
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thats worked a treat. i was originally using Flowlayout (default) and the graph was most probably being drawn outside the bounds of the panel and therefore was not visible. Thanks Craig!
z
 
This tiny ad will self destruct in five seconds.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic