Tuesday, March 25, 2008

Setting the Background Image on JFrame

To set the background image on JFrame, there is no such direct methods as setBackground(). So we have to build our own interfaces to do that. Here is an example which uses the JPanel swing component to set the Background Image.

Click here to view the code

or

Click here : JFrameBackgroundImage

Fig : shows the screen when you run the above code. you need to have an image named "image.jpg" for the background image to be placed in the same directory/folder where the class file resides.

11 comments:

Leonam Souza said...

thanks for your example!

Unknown said...

But i am not able to maximize the screen. so cant view the image.

Unknown said...

I think it would be better if you kept the reference of the image in the JPanel class, instead of creating the image in every call of paintComponent.
>>
Image img = new ImageIcon("image.jpg").getImage();

pari@parimalapriya said...

Awesome! Thats a very useful example.

@Sindhu: The setResizable(false) in the code has to be commented to enable frame resizing.

Unknown said...

thanks for your code

Anonymous said...

Thankies sooooo much!

Neerav said...

Hi I run ur code but its not showing any Image in backgroung an u help me plz......

Cheng kuyleang said...

Thanks!It is easy to understand for me to sharpen my java.

Cheng kuyleang said...

Thanks!It is easy to understand for me to sharpen my java.

Macky said...

@neerav, but everyone should know this--

you have to have the file in your project folder first before your program can recognize the file

Macky said...
This comment has been removed by the author.