Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Quick Question on Images

  1. #1
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Quick Question on Images

    Hello,

    I setting a image using the ImageIcon getResource syntax and I am I having trouble displaying an Image. It works fine when I type the String and not store it in a variable. It displays the picture flawlessly. Now when I try the same thing and store the String into a variable it is unable to fine the file. Let me show you the link of code that is troubling me.

    Icon image= new ImageIcon(getClass().getResource(NameofProduct));

    NameofProduct is the Variable holding the String. See I am using JTextField retrieve method to get the data and Store it in NameofProduct,and I then would like it to display the corresponding picture. I have achieved this using the JComboBox my only hurdle is trying to fiqure out away of using the getResource Method to use the String in the Variable and not the variable itself. I hope this makes sense.


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Quick Question on Images

    My guess is that many people will be wondering if the variable (it should really start with a lower case letter) really does contain what you think it does. Debugging is as much about skepticism as anything else.

    Print the value of string. Also print the icon (including its width and height to make sure it has real data.)

    Consider posting a cut down example illustrating the problem: ie not even a gui, just two image icons created one from a string literal, one from a variable with the text output described above.

    --- Update ---

    My guess is that many people will be wondering if the variable (it should really start with a lower case letter) really does contain what you think it does. Debugging is as much about skepticism as anything else.

    Print the value of string. Also print the icon (including its width and height to make sure it has real data.)

    Consider posting a cut down example illustrating the problem: ie not even a gui, just two image icons created one from a string literal, one from a variable with the text output described above.

  3. #3
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: Quick Question on Images

    More of my code
     jd.setText(NameofProduct);
                    jfield.setText(jtProfitindex1);
                    jtProfit.setText(OrginalPrice);
                    ShippingCost.setText(ShippingCost2);
                    System.out.println(NameofProduct+"the name of the picture ");
                    //the name of picture being stored here
                     Icon image= new ImageIcon(getClass().getResource(NameofProduct+".png"));
                      final   JLabel pictureCombo =new JLabel(image);
                    if(a.equalsIgnoreCase(NameofProduct)){
     
                        picture2.setIcon(image);
                        picture2.setLayout(new FlowLayout(FlowLayout.CENTER));
                    }
                    else{
                          System.out.println(NameofProduct+"the name of the picture ");
                       picture2.setIcon(null);
                        picture2.setLayout(new FlowLayout(FlowLayout.CENTER));  
                    }
                }

    Like I said earlier, i am pulling the data from an Excel sheet and storing it into a JTextfield from there I want to change the picture according to the variable name. I think this process is straight forward but yet it doesn't work. As an example, when the Strings are typed in
        Icon image= new ImageIcon(getClass().getResource("Iphone.png"));
    The image will display and when I select another image using ComboBox the picture will change. Any explanation on what I am missing will greatly be appreciated.

    Thanks

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Quick Question on Images

    it doesn't work
    Can you explain what happens and what doesn't work. Your comment seems to say it works:
    The image will display and when I select another image using ComboBox the picture will change
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: Quick Question on Images

    Alright, let me try to explain in more detail. When I type in the .getResource constructor, the literal String of the image will move with the combobox. It works well with the If and Else statement. This is what I want it to do, but with a variable holding the string itself. Now when I use the JTextField to read in a cell from my Excel sheet and store it into a variable and place that variable into .getResource constructor the image will display intially, but then if I select another item from my ComboList it throws a null Pointer Expection. If I repeat the process, but instead of using the variable but type the name of item into the constructor it works perfectly. Do I need to show more of my code.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Quick Question on Images

    Can you make a small, simple complete program that compiles, executes and shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Jun 2012
    Posts
    105
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: Quick Question on Images

    Hey Norm,

    I had to take a few days to really understand my problem, so I can correctly communicate it to you. See my problem, is I have a set of items in a combobox and I am trying to link items in my combobox to a set of pictures. This could be solved by a simple If and else statement, but see I do not want to hard code it in. I want to be able to add pictures in the future and have the program use the pictures and associate it to the combobox. I could always just add more if and else statements, but I know there is a more efficient way of accomplishing my goal. The problem with my code of that I showed you in the past was I comparing the same thing. See the variable A represents the object from combox I just forced it into a string, and if you look at my JTextfield is the samething as my combobox. So in essence, every time I would select an item from my inventory the first statement of my IF and Else statement would fire. I have to make them independent of each-other. I am having much trouble figuring that out. I have items from my ComboBox that would like to associate pictures with, but the pictures will forever be growing as my business expands hopefully. I hope this better explains my problem. I am tempted to just write a ton of If and Else statements, but the desire to grow far out exceeds my laziness.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Quick Question on Images

    Do you have some code that shows the problem?
    Code that will compile and execute.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Quick question
    By l1nk3 in forum Java Theory & Questions
    Replies: 1
    Last Post: February 24th, 2012, 02:02 PM
  2. quick question, please HELP!
    By Nemus in forum What's Wrong With My Code?
    Replies: 14
    Last Post: September 29th, 2011, 01:23 PM
  3. Quick Question
    By sird00dguyman in forum Java Theory & Questions
    Replies: 2
    Last Post: August 4th, 2011, 06:14 PM
  4. Another Quick Question
    By Jacksontbh in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 1st, 2011, 07:18 AM
  5. Hi, quick question
    By curras in forum Member Introductions
    Replies: 1
    Last Post: March 21st, 2011, 03:21 PM