• 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

GUI and ImageIcon

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I am having a difficult time displaying the images in an image folder in the src folder in Netbeans. I have looked everywhere and I know that the grammar in this case is correct. However, the image is not showing. *Note that there are 52 images(52 cards) in the image folder
I have tried the following code and it still does not work:*please note that the path is correct and this is the format it needs to be, I am thinking that it is not displaying the image because I am doing a minor thing incorrectly which I cannot identify.
proper format which i have recreated in my code to meet this 'criteria' : ImageIcon imageIcon = new ImageIcon("images/2C.png");


 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Please UseCodeTags (← click) when posting code as it makes it easier for people to read your code. I've added them for you this time.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know you said the path is correct but the first thing I would do is create a File object for "images/"+stringCardNumber+stringCharCard+".png" and then print out the result of calling its exists() and getAbsolutePath() methods to see if the relative path is resolving to where you think it should be resolving to.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ignore that last reply, I've just noticed you are adding the JLabel dynamically. You need to revalidate the panel the JLabel has been added to.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:Welcome to the Ranch.

Welcome again Please UseCodeTags (← click) when posting code as it makes it easier for people to read your code. . . .UNfortunately there was a comment far too long which I have shortened by removing ----------------------------------
The method in that Listener class looks too long and complicated, and I think you should divide it into different methods. That will also make debugging easier.
 
Rez Sad
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:Ignore that last reply, I've just noticed you are adding the JLabel dynamically. You need to revalidate the panel the JLabel has been added to.



I tried to do the revalidate method on my panel but it did not change the results. It is still not showing an image.

Also thanks everyone and thanks for the post fix!
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right, I think you need a bit of debugging. There is probably something going wrong in that actionPerformed method. I thought it looked long and complicated. Separate the line where you are seeking the image files. Print out the path you are using. Give the ImageIcon object a variable name and print whether that is null.
 
Rez Sad
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is not null, I have debugged it and according to the debugger, everything is working perfectly.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In which case the debugger is mistaken
Get the JVM to print the PATH to the image, and the image object (I presume its toString method will print it in a few words). Similarly print the JLabel imageCard; it should tell you whether it has the image on.
I do not like adding things to a display in a method like that; you are liable to get a strange display and it will change shape every time you click the button. give that label a different colour background from the others, so you can see where you are adding it.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I tried to do the revalidate method on my panel but it did not change the results. It is still not showing an image.


I just modified your code to add a label with the word "TEST" in it instead of an image and when I added a call to revalidate() on the panel the JLabel appears.

I good way to test if it just a validation issue is to resize the panel. If when you resize the panel the images appear the problem is a validation issue, if they still do not appear then the problem is with the images.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might be better using the same label throughout and its setIcon method
 
Rez Sad
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found the problem. The first problem is the first catch


for some reason I needed to change the 'Exception' to 'InvalidParameterException' (the same exception I used in my card class)


The 2nd problem i found was that the images folder that the program was searching for was not in the 'src' folder.. it was looking inside the main folder directory for some mysterious reason...
even though I do not believe it matters as from what I understand the program can just dig in the sub-folders for the images folder....

Just made the post to inform you guys on what happened and thank you all so very much for your help!
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rez Sad wrote: . . . I needed to change the 'Exception' to 'InvalidParameterException' . . .

What difference will that make?

. . . the images folder that the program was searching for was not in the 'src' folder.. . . .

That is why the Java Tutorials tells you to use the get resource methods of the Class class to find an image.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic