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?
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).