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.
Re: Quick Question on Images
More of my code
Code java:
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 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
Re: Quick Question on Images
Can you explain what happens and what doesn't work. Your comment seems to say it works:
Quote:
The image will display and when I select another image using ComboBox the picture will change
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.
Re: Quick Question on Images
Can you make a small, simple complete program that compiles, executes and shows the problem?
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.
Re: Quick Question on Images
Do you have some code that shows the problem?
Code that will compile and execute.