aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes nested components and sizing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "nested components and sizing" Watch "nested components and sizing" New topic
Author

nested components and sizing

Paul Rhoades
Greenhorn

Joined: Apr 15, 2005
Posts: 2
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.
Giovanni De Stefano
Ranch Hand

Joined: Aug 17, 2004
Posts: 150
Try to override the public void paint(Graphics g) method of the component.

I hope this helps

Giovanni


SCJP 1.4
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
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).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: nested components and sizing