Dear All,

In Java Swing, for all components there is a setSize method which takes int height and width as arguments.

And we will pass parameters like 600,500 & 400,300 etc...

My doubt is what is the unit of these height and width . I mean on what unit, the size of the component is rendered. Is it Centimeter,mm,pixels ?

Im not sure how these numbers(height and width) contribute to the rendering of the component.

Please throw light on it.

Recommended Answers

All 6 Replies

It's pixels

If you are using a layout manager like you should, then setSize is pretty much useless. With a null layout manager it sets the component size in pixels, and you look really silly when someone runs it on a retina display.

Hi James,

Could you please explain why Setsize is useless when using layout manager.

For any Component like Jpanel, Jframe we will use the layout managers and set size using SetSize method only. Then why it is useless. Without setting the size how will the component get rendered on screen.

Please explain.

Most components calculate a preferred size based on their contents, eg the text in a JLabel or the overall size of the children of a JPanel. Most layout managers use getPreferredSize rather than getSize. The layout manager will then often override that to make the components fit according to its own rules (eg all the components in a Grid are the same size). On average you are more likely to influence the layout manager by setting a minimum size.
If mKorbel is around he's a real expert on this stuff...

oh ok, just for confirmation, these getPreferedsize or any method that influences the size also uses pixels as Units for the parameters right ?

Yes, but of course components calulate their preferred size using the metrics of their current font etc, so they scale properly with different environments.

Thanks James !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.