• 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

nested components and sizing

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have a ui that is made up of nested components. the problem i have is that the size of the nested components depends on the font size which i think i get from Graphics passed into paintComponent. so, how do i work out the size of a component prior to painting it? it seems like i have to paint all child components to workout their sizes and then repaint them after re-locating them? is there another way?

regards

P.
 
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try to override the public void paint(Graphics g) method of the component.

I hope this helps

Giovanni
 
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 'component' way to do this is with the appropriate (combination of) layout manager(s) for your component(s) and to use a JLabel, perhaps in conjunction with a JScrollPane. This way java will do all the location and rendering of text and component/ui resizing work for you.

Another way is the 'graphics' way. This takes a little more work and care. To get the size of text strings you have options: methods in the Font class or in the TextLayout class with possible use of the LineBreakMeasurer class (api has example code) in addition. These use the Graphics g object passed in the argument of the paintComponent method, as you mentioned. Set you font and use the methods in the classes to get the information you need to position and draw your text.

If your graphic component is in a JScrollPane you can provide size information for Scrollable updates by overriding the getPreferredSize method and returning the desired/changing Dimension. You can trigger the (JScrollPane) update by calling revalidate on your JComponent from within your event code.

how do i work out the size of a component prior to painting it
Your gui components will get laid out and be able to return useful size and location information after you call pack or setVisible on their top–level container (usually, JFrame).
reply
    Bookmark Topic Watch Topic
  • New Topic