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