So what was the answer? Please always ost a final post to any thread so other people can see your solution and use it for their own problems. A link to the other thread would, of course, be a satisfactory alternative.
Joined: Jun 05, 2006
I used setPreferredSize(with,height) but it did not work. The whole image is not being displayed using it. Here is my code
Java provides us with so many options that it's hard to say too much about the ways you can do something. Of course you can add an ImageIcon to a JLabel and add it to a JPanel; that's easy. But when you start talking about a background image thoughts naturally turn toward a graphics approach. It's better to not use a separate component for the background image; you won't be able to add any other components on top of it. Draw your image on the component that will host your login components. Then you can use the component as you would any other: add components, set borders, etc. Since you're adding components to your login panel you don't need to be concerned with the size of the image, the panels layout manager will calculate the preferred size from the components and layout constraints. Setting the preferred size of a graphic component is important when it has no child components (which the layout manager can query to determine the preferred size). Here is a general approach to drawing a background image on a component with some options to scale or not scale.