Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem relating to jpanel background image

 
borj bersas
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2584
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're not showing us your code.

Here is my code:
 
Rob Spoor
Sheriff
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're obviously missing the call to super.paintComponent(g) as the first line of your paintComponent method.
 
borj bersas
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
where should I put these: super.paintComponent(g);
 
borj bersas
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2584
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2584
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic