Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Image does not load on JPanel, no errors  RSS feed

 
Deborah Larsson
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I tested a class I created to load images and that works fine. When calling the class with 'new LoadImage("imagename.gif");' from another class it fails.
I really have no idea why..

Thank you ahead for your help.

 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should start using resources. Check out Class.getResource and ClassLoader.getResource. Moving to our GUI forum.
 
Rob Camick
Ranch Hand
Posts: 2801
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, there is no need to create a custom component to do this. Just use a JLabel with an Icon.

I suggest you read the Swing tutorial for working example. Start with the section on "How to Use Icons". And if you really must to custom painting then read the section of custom painting. YOu should not be extending Component since that is an AWT component and you should only use Swing components in a Swing application.

 
Deborah Larsson
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:You should start using resources. Check out Class.getResource and ClassLoader.getResource. Moving to our GUI forum.

Thanks for the tip, but that was not helpful for solving the problem.
 
Deborah Larsson
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:Also, there is no need to create a custom component to do this. Just use a JLabel with an Icon.

I suggest you read the Swing tutorial for working example. Start with the section on "How to Use Icons". And if you really must to custom painting then read the section of custom painting. YOu should not be extending Component since that is an AWT component and you should only use Swing components in a Swing application.



I am not sure why you advise me to use a JLabel for this. Easier than adding to a panel with no undesirable side-effects? It is a normal image without text. It also worked on itself, via it's main function, so the code itself was ok.
I solved the issue now. It did not show because by default it used a grouplayout manager when it was created with the GUI designer in Netbeans . In the end I had to open it via an external editor to delete the group layout code. Then via the designer it finally accepted the change to a borderLayout. and did the trick.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deborah Larsson wrote:
I am not sure why you advise me to use a JLabel for this. Easier than adding to a panel with no undesirable side-effects? It is a normal image without text.

Yes, the code to do this with a simple JLabel/ImageIcon is a lot simpler than what you're currently doing.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!