Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

adding graphics to panels  RSS feed

 
zack chancery
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • 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
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!