• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem relating to jpanel background image

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You're not showing us your code.


Here is my code:
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
where should I put these: super.paintComponent(g);
 
borj bersas
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Remember to always leap before you look. But always take the time to smell the tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic