This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes problem relating to jpanel background image Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "problem relating to jpanel background image" Watch "problem relating to jpanel background image" New topic
Author

problem relating to jpanel background image

borj bersas
Greenhorn

Joined: Apr 19, 2010
Posts: 20
i tried this code:

code

but there is nothing in it, i really want to have a background image for my jpanel.

what i did:

1.create a blank class
2.copy paste the code
3.change the class name to my class name
4.no error--->run

output:

empty window.

note:
Penguins.jpg is located at the "images" package of my project, so i have the right image path.

what seems to be the problem here?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2155
    
    7
Your program can't find the image.

1) Read the section from the Swing tutorial on How to Use Icons for a better way to load images.

2) There is no need to creat a custom class for this. You can just use a JLabel for the icon.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
borj bersas wrote:i tried this code:

code

but there is nothing in it, i really want to have a background image for my jpanel.

The code worked for me.


what seems to be the problem here?

You're not showing us your code.
borj bersas
Greenhorn

Joined: Apr 19, 2010
Posts: 20
Rob Camick wrote:Your program can't find the image.

1) Read the section from the Swing tutorial on How to Use Icons for a better way to load images.

2) There is no need to creat a custom class for this. You can just use a JLabel for the icon.


I use JPanel because i want to add buttons, tables and labels in that JPanel
borj bersas
Greenhorn

Joined: Apr 19, 2010
Posts: 20
You're not showing us your code.

Here is my code:
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

You're obviously missing the call to super.paintComponent(g) as the first line of your paintComponent method.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
borj bersas
Greenhorn

Joined: Apr 19, 2010
Posts: 20
where should I put these: super.paintComponent(g);
borj bersas
Greenhorn

Joined: Apr 19, 2010
Posts: 20
I did put these: super.paintComponent(g); at the first line as you said but still i cant see the image
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2155
    
    7
I use JPanel because i want to add buttons, tables and labels in that JPanel


You can add buttons, table, etc. to a JLabel as well. Just set the layout manager you want to use.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
borj bersas wrote:
You're not showing us your code.

Here is my code:


Rob was right, you're probably looking in the wrong place for the image. What if you do this instead:

borj bersas
Greenhorn

Joined: Apr 19, 2010
Posts: 20
Rob Camick wrote:
You can add buttons, table, etc. to a JLabel as well. Just set the layout manager you want to use.

how do I do that? I'm just starting in java programming hehe

can you make a running code for that so I can copy-paste it in java empty class? tnx in advance
borj bersas
Greenhorn

Joined: Apr 19, 2010
Posts: 20
pete stein wrote:
Rob was right, you're probably looking in the wrong place for the image. What if you do this instead:



it worked ^_^ hehe. tnx
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2155
    
    7
how do I do that? I'm just starting in java programming hehe


Its exactly the same as using a JPanel. What did you try and what problems did you have? This is how you learn when you are a beginner. You try things. If they don't work as expected then you post the code you used with a question and we will help you out.

can you make a running code for that so I can copy-paste it in java empty class? tnx in advance


No. You can read the section from the Swing tutorial on Using Layout Managers for examples.

Why do you think I gave you the llink above on "How to Use Icons". Too bad you where given the answer, now you didn't bother to read the section I pointed you to and you also didn't read the layout manager section on your own either.

Tutorial exist for you to read them and learn the basics.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: problem relating to jpanel background image
 
Similar Threads
avoiding flickering
Inserting an Image in JTextField
Printing a BufferedImage
3 questions regarding JPanel.
Swing Background Picture