Problem of putting images on Labels

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 857 Views - Last Post: 17 February 2010 - 01:30 AM Rate Topic: -----

#1 Guest_Jolin*


Reputation:

Problem of putting images on Labels

Posted 14 February 2010 - 07:56 PM

Hello everybody! I have the above code and i want to put an Image to a specific Label on a frame
I have created eight Labels with empty Images but i want to change these images when the user
chooses a drink.
for example if the user choose to buy tea, an Image should appear to Label1,
if he choose tea again,an Image should appear to Label2 and so on while user has no money left.

I want the Images to appear in the order tea, milk, soda from left to right no matter the
combination of drinks the user choose...

i tried the above with no luck...

JPanel drinks = new JPanel();
JLabel label1 = new JLabel();
label1.setIcon(new ImageIcon("IMAGES/blank.png"));
drinks.add(label1);
this.add(drinks,BorderLayout.SOUTH);

I change the Image in the switch statement bellow but i cant put
the Images in the right order and i can not cover all situations.

thank you in advance.



Object [] drinks ={"tea","milk","soda","leave shop"};

// the total amount is 100 soda has 40 , milk has 30 , soda has 15...
int drinksAmount = 100;

boolean p = true;

while(p)
{

int choise = JOptionPane.showOptionDialog(null,
"You Have "+drinksAmount+" Euro to spend",
"Select purchache!",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null,drinks,drinks[0]);

switch (choise) //switch the cases and checking the acount balance...
{
case 0:

if (drinksAmount >= 40)
{
drinksAmount = drinksAmount - 40;
}
else
{
JOptionPane.showMessageDialog(null,
"no money to spend!",
"error",
JOptionPane.ERROR_MESSAGE);
}

break;

case 1:

if (drinksAmount >= 30)
{
drinksAmount = drinksAmount -30;
}
else
{
JOptionPane.showMessageDialog(null,
"no money to spend!",
"error",
JOptionPane.ERROR_MESSAGE);
}
break;

case 2:

if (drinksAmount >=15)
{
drinksAmount = drinksAmount - 15;
}
else
{
JOptionPane.showMessageDialog(null,
"no money to spend!",
"error",
JOptionPane.ERROR_MESSAGE);
}
break;

case 3:
p = false;
break;

default:
System.exit(0);
}
}



Is This A Good Question/Topic? 0

Replies To: Problem of putting images on Labels

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Problem of putting images on Labels

Posted 14 February 2010 - 08:07 PM

Please add the code tags around your code:

:code:

Thanks!
Was This Post Helpful? 0
  • +
  • -

#3 Jolin  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 10
  • Joined: 14-February 10

Re: Problem of putting images on Labels

Posted 14 February 2010 - 08:15 PM

Object [] drinks ={"tea","milk","soda","leave shop"};

// the total amount is 100 tea has 40 , milk has 30 , soda has 15...
int drinksAmount = 100;

boolean p = true;

while(p)
{

int choise = JOptionPane.showOptionDialog(null,
"You Have "+drinksAmount+" Euro to spend",
"Select purchache!",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null,drinks,drinks[0]);

switch (choise) //switch the cases and checking the acount balance...
{
case 0:

if (drinksAmount >= 40)
{
drinksAmount = drinksAmount - 40;
}
else
{
JOptionPane.showMessageDialog(null,
"no money to spend!",
"error",
JOptionPane.ERROR_MESSAGE);
}

break;

case 1:

if (drinksAmount >= 30)
{
drinksAmount = drinksAmount -30;
}
else
{
JOptionPane.showMessageDialog(null,
"no money to spend!",
"error",
JOptionPane.ERROR_MESSAGE);
}
break;

case 2:

if (drinksAmount >=15)
{
drinksAmount = drinksAmount - 15;
}
else
{
JOptionPane.showMessageDialog(null,
"no money to spend!",
"error",
JOptionPane.ERROR_MESSAGE);
}
break;

case 3:
p = false;
break;

default:
System.exit(0);
}
}

This post has been edited by Jolin: 15 February 2010 - 04:51 AM

Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,032
  • Joined: 15-July 08

Re: Problem of putting images on Labels

Posted 14 February 2010 - 09:18 PM

I can't give you the code, but I suggest making an array of Icons that you can then set in the JLabel using the jLabel.setIcon(icon_image) method in the appropriate switch statements.

Here is one of the best Icon/JLabel tutorials that I could find, and better yet, it is written by Sun itself.
Was This Post Helpful? 0
  • +
  • -

#5 Jolin  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 10
  • Joined: 14-February 10

Re: Problem of putting images on Labels

Posted 15 February 2010 - 04:22 AM

View PostDogstopper, on 14 February 2010 - 08:18 PM, said:

I can't give you the code, but I suggest making an array of Icons that you can then set in the JLabel using the jLabel.setIcon(icon_image) method in the appropriate switch statements.

Here is one of the best Icon/JLabel tutorials that I could find, and better yet, it is written by Sun itself.


Thank you for your reply, well i am not asking for the code but for a solution...
the problem is that if i put the icons with the method you suggest i can not cover
all the situations for example if the user buy tea, then milk and then soda
everything will work fine..but what if he by two sodas and then a milk?
in this case a milk icon should appear in Label1 a soda icon in label2
and a soda icon in label3 and so on... how could i do this?

i have also tried to put the icons by calculating the drinksAmount in the cases above
 if (drinksAmount==10)
       {
          a.setIcon(milkIcon);
          b.setIcon(sodaIcon);
          c.setIcon(sodaIcon);
          //and so on... but the drink amount could be 10 more than
          // one times so it doesn't work..

       }


This post has been edited by Jolin: 15 February 2010 - 04:31 AM

Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,032
  • Joined: 15-July 08

Re: Problem of putting images on Labels

Posted 15 February 2010 - 05:15 AM

If that is the case, then maybe a switch statement is not the best thing to use. I'm assuming that the switch tests for which choice the user picked. However, it works like an if, else if, else statement works and only ONE of the options are picked. If you want to make multiple choices, you need to think about looping. Also, as I said, use an array of JLabels and make a new one when needed.
Was This Post Helpful? 0
  • +
  • -

#7 Jolin  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 10
  • Joined: 14-February 10

Re: Problem of putting images on Labels

Posted 15 February 2010 - 05:42 AM

View PostDogstopper, on 15 February 2010 - 04:15 AM, said:

If that is the case, then maybe a switch statement is not the best thing to use. I'm assuming that the switch tests for which choice the user picked. However, it works like an if, else if, else statement works and only ONE of the options are picked. If you want to make multiple choices, you need to think about looping. Also, as I said, use an array of JLabels and make a new one when needed.


well here is the code i have done till now.

Object[]drinks={"tea","milk","soda","Leave Shop"};

JLabel a = new JLabel();
a.setIcon(new ImageIcon("IMAGES/empty.png"));

JLabel b = new JLabel();
b.setIcon(new ImageIcon("IMAGES/empty.png"));

JLabel c = new JLabel();
c.setIcon(new ImageIcon("IMAGES/empty.png"));

JLabel d = new JLabel();				//if the user Leave the shop no image sould appear...
d.setIcon(new ImageIcon("IMAGES/empty.png"));

JLabel e = new JLabel();
e.setIcon(new ImageIcon("IMAGES/empty.png"));

JLabel f = new JLabel();
f.setIcon(new ImageIcon("IMAGES/empty.png"));

JLabel g = new JLabel();
g.setIcon(new ImageIcon("IMAGES/empty.png"));
		
JLabel h = new JLabel();
h.setIcon(new ImageIcon("IMAGES/empty.png"));
		
ImageIcon teaIcon = new ImageIcon ("IMAGES/tea.PNG");
ImageIcon milkIcon = new ImageIcon ("IMAGES/milk.PNG");	//creating new images.
ImageIcon sodaIcon = new ImageIcon ("IMAGES/soda.PNG");


int drinksAmount = 100; 	// the money user has.

boolean p = true;
while(p)
{																											
	 
int choise = JOptionPane.showOptionDialog(null, 
            "You Have "+drinksAmount+" Euro",      
            "Select purchache!",
            JOptionPane.YES_NO_CANCEL_OPTION,
            JOptionPane.INFORMATION_MESSAGE,
            null,shop,shop[0]);           
                     
switch (choise)
{
  case 0:
  	a.setIcon(teaIcon);
	if (drinksAmount >= 40)
	{
 		 drinksAmount = drinksAmount - 40;
	}
	else
	{
		JOptionPane.showMessageDialog(null,
		"no money to spend!",
		"error",
		JOptionPane.ERROR_MESSAGE);
	} 
	if (drinksAmount==20)
	{
	b.setIcon(teaIcon);	
	}
										//switch the cases
  break;
  
  case 1:

	if (drinksAmount >= 30)
	{ 
		drinksAmountt = drinksAmount -30;
	}
 	else
	{
		JOptionPane.showMessageDialog(null,
		"no money to spend!",
		"error",
		JOptionPane.ERROR_MESSAGE);
	}
	if (drinksAmount==70)
	{
	a.setIcon(milkIcon);	
	}
	else if (drinksAmount==40);
	{
	c.setIcon(sodaIcon);	
	}
  break;
   
  case 2:
  c.setIcon(sodaIcon);
	if (drinksAmount >=15)
  	{
  		drinksAmount = drinksAmount- 15;
	}
	else
	{
  		JOptionPane.showMessageDialog(null,"no money to spend!","error",JOptionPane.ERROR_MESSAGE);  
	} 
  break;
  
  case 3:
  p = false;
  break;
  
  default:
  System.exit(0);
  break;
  }
}

JPanel icons = new JPanel();
icons.setLayout(new GridLayout(2,4,1,1));
icons.setBackground(Color.gray);
icons.add(a);
icons.add(B)/>;
icons.add(c);
icons.add(d);
icons.add(e);
icons.add(f);
icons.add(g);
icons.add(h);
this.add(icons,BorderLayout.SOUTH);



The problem is that i do not know what to loop in order to achive
what i want! any suggestions? PLEASE!

This post has been edited by Jolin: 15 February 2010 - 05:47 AM

Was This Post Helpful? 0
  • +
  • -

#8 Jolin  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 10
  • Joined: 14-February 10

Re: Problem of putting images on Labels

Posted 15 February 2010 - 01:04 PM

well i found the solution (no arrays && no loops...and offcourse without putting away the switch statement..) :bananaman:

I wonder how an expert could not answer a simple question like this... :wacko:

Anyway thank you for your time..... ^^
Was This Post Helpful? -2
  • +
  • -

#9 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,032
  • Joined: 15-July 08

Re: Problem of putting images on Labels

Posted 15 February 2010 - 02:48 PM

but...that's almost exactly what I told you to do...The only thing I said is it might be BETTER to use ifs; didn't say it wouldn't work...

Now you made me cry :cry2:

...
Was This Post Helpful? 0
  • +
  • -

#10 Jolin  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 10
  • Joined: 14-February 10

Re: Problem of putting images on Labels

Posted 15 February 2010 - 03:08 PM

i post that by mistake...

This post has been edited by Jolin: 15 February 2010 - 03:22 PM

Was This Post Helpful? 0
  • +
  • -

#11 Jolin  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 10
  • Joined: 14-February 10

Re: Problem of putting images on Labels

Posted 15 February 2010 - 03:16 PM

View PostDogstopper, on 15 February 2010 - 01:48 PM, said:

but...that's almost exactly what I told you to do...The only thing I said is it might be BETTER to use ifs; didn't say it wouldn't work...

Now you made me cry :cry2:

...


I am sorry to hear that :sad3:

Thank you again for your time but you maybe could help me now..
the only thing i did is to put
three more int variables:
int count1 =0;
int count2 =0;
int count3 =0;

and then i use some if statements 

so i have done something like this[code]
if (count1==1 && count2==2 && count3==2)
  {
    a.setIcon(teaIcon);		
    b.setIcon(milkIcon);	
    c.setIcon(sodaIcon);
    d.setIcon(sodaIcon);
    e.setIcon(sodaIcon);
  }




this is a bad headache (to check all the cases)
but it was the only way I though...
if you have something else in your mind please let me
know about it.. thanks anyway...! :)

This post has been edited by Jolin: 15 February 2010 - 04:14 PM

Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,236
  • Joined: 27-December 08

Re: Problem of putting images on Labels

Posted 15 February 2010 - 03:38 PM

Your errors are caused by two things. First, the statement int count++; is illegal because you are incrementing a variable which has not yet been initialized. In addition, by declaring your count variables inside your switch statement, they are local to it. This means that they cannot be invoked outside of the switch block.
Was This Post Helpful? 0
  • +
  • -

#13 Jolin  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 10
  • Joined: 14-February 10

Re: Problem of putting images on Labels

Posted 15 February 2010 - 04:13 PM

View Postmacosxnerd101, on 15 February 2010 - 02:38 PM, said:

Your errors are caused by two things. First, the statement int count++; is illegal because you are incrementing a variable which has not yet been initialized. In addition, by declaring your count variables inside your switch statement, they are local to it. This means that they cannot be invoked outside of the switch block.


I am not getting any errors.. things are working perfect..

i initialize the variables before the switch statement.
if (choise==0)
{
count1++;	
}
if (choise==1)
{
count2++;
}       
if (choise==2)
{
count3++;	
}           



but i wonder if there is any better approach..

This post has been edited by Jolin: 15 February 2010 - 04:15 PM

Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,236
  • Joined: 27-December 08

Re: Problem of putting images on Labels

Posted 15 February 2010 - 04:30 PM

I'm confused now. Are you asking if it is better to use a chain of if statements or a switch statement? Because in your most recent post, you refer to this block of if statements as a switch, which is inaccurate.
if (choise==0) 
{ 
count1++;        
} 
if (choise==1) 
{ 
count2++; 
}        
if (choise==2) 
{ 
count3++;        
}          



Now in terms of declaring and initializing, even if you declare the variables above the switch, but declare new variables with the same names inside of it, the most local variables will overshadow the more global variables. Here's what I mean:
int x = 1;
int count = 0;
switch(x){
   case 1:
      //this count variable overshadows
      //the one outside of the switch block
      //and takes precedence when referencing
     int count = 1; 
}


Was This Post Helpful? 0
  • +
  • -

#15 Jolin  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 10
  • Joined: 14-February 10

Re: Problem of putting images on Labels

Posted 15 February 2010 - 05:11 PM

well to make thinks clear.

i have 3 ImageIcons one for each Object(tea,milk,soda) and the user can buy any combination of them.
at first user has 100 Euro in his acount. first off all i have to calculate the money the user has
after he buys one Item or more. I had no problem with that.

then for every Object the user has i want to put an Image on a JLabel
but i want the Images to appear in the order tea,milk,soda no matter
which item the user choose to buy first.

I also want the order of the Images to be from left to right for example:
if the user buy only milk then the milkIcon should appear in Label1 instead of
Label2 and so on...

i deside to use a switch statement in order to get what the user choose
and i tried to put the images in the write order using the drinksAmount variable.
but because this variable could return the same number more than one times it does not work
for all the cases.

so i decide to use three more variables in order to fix that.. the count variables
here is the code until now:
Object []shop={"tea","milk","soda","leave store"};													// String to hold the Options.

JLabel a = new JLabel();
a.setIcon(new ImageIcon("IMAGES/empty.png"));

JLabel b = new JLabel();
b.setIcon(new ImageIcon("IMAGES/emty.png"));

JLabel c = new JLabel();
c.setIcon(new ImageIcon("IMAGES/empty.png"));

JLabel d = new JLabel();
d.setIcon(new ImageIcon("IMAGES/empty.png"));

JLabel e = new JLabel();
e.setIcon(new ImageIcon("IMAGES/empty.png"));

JLabel f = new JLabel();
f.setIcon(new ImageIcon("IMAGES/empty.png"));

JLabel g = new JLabel();
g.setIcon(new ImageIcon("IMAGES/empty.png"));
		
JLabel h = new JLabel();
h.setIcon(new ImageIcon("IMAGES/empty.png"));
ImageIcon emptyIcon  = new ImageIcon ("IMAGES/empty.png");
ImageIcon teaIcon = new ImageIcon ("IMAGES/tea.PNG");
ImageIcon milkIcon = new ImageIcon ("IMAGES/milk.PNG");
ImageIcon sodaIcon = new ImageIcon ("IMAGES/soda.PNG");


int drinksAmount = 100; 
int count1 = 0;
int count2 = 0;
int count3 = 0;

boolean p = true;
while(p)
{																											
	 
int choise = JOptionPane.showOptionDialog(null, 
            "You Have "+drinksAmount+" Euro",      
            "Select purchache!",
            JOptionPane.YES_NO_CANCEL_OPTION,
            JOptionPane.INFORMATION_MESSAGE,
            null,shop,shop[0]);  

if (choise==0)
{
count++;	
}
if (choise==1)
{
prot++;
}       
if (choise==2)
{
count3++;	
}           
            
            
            
            
         
                     
switch (choise)
{
  case 0:

	if (drinksAmount >= 40)
	{
		drinksAmount = drinksAmount - 40;	
	}
	else
	{
		JOptionPane.showMessageDialog(null,
		"no money left!",
		"error",
		JOptionPane.ERROR_MESSAGE);
	} 
	if (count1==1)
	{
	a.setIcon(teaIcon);	
	}
	else if (count==2)
	{
	b.setIcon(teaIcon);	
	}
	
  break;
  
  case 1:
 
	if (drinksAmount >= 30)
	{ 
		drinksAmount = drinksAmount -30;
	}
 	else
	{
		JOptionPane.showMessageDialog(null,
		"no money left!",
		"error",
		JOptionPane.ERROR_MESSAGE);
	}

if (count2==1)
	{
	a.setIcon(milkIcon);	
	}
else if (count2==2)
	{
	b.setIcon(milkIcon);
	}
else if (count2==3)
	{
	c.setIcon(milkIcon);
	}	
  break;
   
  case 2:
 
	if (drinksAmount >=15)
  	{
  		drinksAmount = drinksAmount - 15;
	}
	else
	{
  		JOptionPane.showMessageDialog(null,"you dont have any money!","error",JOptionPane.ERROR_MESSAGE); 		 
	} 
	if (count3==1)
	{
	a.setIcon(sodaIcon);	
	}
	else if(count3==2)
	{
	b.setIcon(sodaIcon);	
	}
	else if (count3==3)
	{
	c.setIcon(sodaIcon);	
	}
	else if (count3==4)
	{
	d.setIcon(sodaIcon);	
	}
	else if (count3==5)
	{
	e.setIcon(sodaIcon);	
	}
	else if (count3==6)
	{
	f.setIcon(sodaIcon);	
	}
	
  break;
  
  case 3:
  p = false;
  break;
  
  default:
  System.exit(0);
  break;
  }
}
if (drinksAmount==15)
{
a.setIcon(teaIcon);
b.setIcon(milkIcon);
c.setIcon(sodaIcon);	
}
if (drinksAmount==30)
{
a.setIcon(teaIcon);
b.setIcon(milkIcon);	
}
if (drinksAmount==5)
{
a.setIcon(teaIcon);
b.setIcon(teaIcon);
c.setIcon(sodaIcon);	
}
if (count2==2 && count3==1 && goldAmount==25)
{
a.setIcon(milkIcon);
b.setIcon(milkIcon);
c.setIcon(sodaIcon);	
}
if (count3==2 && count2 == 1 && count1==1)
{
a.setIcon(teaIcon);
b.setIcon(milkIcon);
c.setIcon(sodaIcon);
d.setIcon(sodaIcon);		
}
if (count3==6)
{
a.setIcon(milkIcon);
b.setIcon(milkIcon);
c.setIcon(sodaIcon);
d.setIcon(sodaIcon);	
}
if (count3==4 && count2==1)
{
a.setIcon(milkIcon);
b.setIcon(sodaIcon);
c.setIcon(sodaIcon);
d.setIcon(sodaIcon);
e.setIcon(sodaIcon);	
}
if (count3==3&& count2==1)
{
a.setIcon(milkIcon);
b.setIcon(sodaIcon);
c.setIcon(sodaIcon);
d.setIcon(sodaIcon);	
}
// and so on.........



is there any better way rather than if statement?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2