change an icon with an action listener

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kalar
    New Member
    • Aug 2007
    • 82

    change an icon with an action listener

    i make a JFrame window and i add a label i.e. myLabel , which has an image and down from the label a button i.e. dice. I want when i press the button to change the image on the label but when i am trying to compile i got an error on ActionListener.

    In ActionListener i have something like this:
    Code:
    myLabel.setIcon= icon2;
    and got error : cannot find symbol variable myLabel

    So what can i do in order to change the icon on the label?
    Thanks!
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    Originally posted by kalar
    i make a JFrame window and i add a label i.e. myLabel , which has an image and down from the label a button i.e. dice. I want when i press the button to change the image on the label but when i am trying to compile i got an error on ActionListener.

    In ActionListener i have something like this:
    Code:
    myLabel.setIcon= icon2;
    and got error : cannot find symbol variable myLabel

    So what can i do in order to change the icon on the label?
    Thanks!
    Maybe you forgot to read this.

    hope it helps,
    sukatoa

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by kalar
      i make a JFrame window and i add a label i.e. myLabel , which has an image and down from the label a button i.e. dice. I want when i press the button to change the image on the label but when i am trying to compile i got an error on ActionListener.

      In ActionListener i have something like this:
      Code:
      myLabel.setIcon= icon2;
      and got error : cannot find symbol variable myLabel

      So what can i do in order to change the icon on the label?
      Thanks!
      The compiler tells you that variable 'myLabel' isn't in scope. There are more errors
      though but first you have to change the visibility of that variable. The little information
      you gave us makes it impossible for us to say anything else about the matter.

      kind regards,

      Jos

      Comment

      • kalar
        New Member
        • Aug 2007
        • 82

        #4
        So my code:
        Code:
        public class Dice extends JFrame implements ActionListener
        {
        	public static final int WIDTH = 200;
        	public static final int HEIGHT = 200;
                
        
            public Dice() 
            {
            	super();
            	
            	setSize(WIDTH, HEIGHT);
        	    addWindowListener(new WindowDestroyer());
        	    
            	ImageIcon icon1 = new ImageIcon("dice8.gif");
            	
            	
            	getContentPane().setLayout(new BorderLayout());
            	JLabel  myLabel = new JLabel(icon1);
            	getContentPane().add(myLabel, BorderLayout.NORTH);
            	
            	JButton button1 = new JButton("Dice");
                button1.addActionListener(this);
                getContentPane().add(button1, BorderLayout.SOUTH);
            }
                
                public void actionPerformed(ActionEvent e)
               { //first i want to test it and after i enable random......
               	//int val = (int)(6*Math.random() + 1);
               	ImageIcon icon2 = new ImageIcon("1.jpg");
               	
                myLabel.setIcon= icon2;
               	
               }
                
        
            public static void main(String[] args) 
            {
            	Dice w1 = new Dice();
            	w1.setVisible(true);
               
            }
        }

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Your variable 'myLabel' is a local variable of your constructor method so it most
          certainly can't be found in another method. Make your variable a member variable
          (local to the instantiation of your class).

          kind regards,

          Jos

          Comment

          • kalar
            New Member
            • Aug 2007
            • 82

            #6
            @Jos
            i make changes but i still have problem
            Code:
            import javax.swing.*;
            import java.awt.*;
            import java.awt.event.*;
            
            
            public class Dice extends JFrame implements ActionListener
            {
            	public static final int WIDTH = 200;
            	public static final int HEIGHT = 200;
                public JFrame myLabel;
            
                public Dice() 
                {
                	super();
                	
                	setSize(WIDTH, HEIGHT);
            	    addWindowListener(new WindowDestroyer());
            	    
                	ImageIcon icon1 = new ImageIcon("dice8.gif");
                	
                	
                	getContentPane().setLayout(new BorderLayout());
                	JLabel  myLabel = new JLabel(icon1);
                	getContentPane().add(myLabel, BorderLayout.NORTH);
                	
                	JButton button1 = new JButton("Dice");
                    button1.addActionListener(this);
                    getContentPane().add(button1, BorderLayout.SOUTH);
                }
                    
                    public void actionPerformed(ActionEvent e)
                   {
                   	//int val = (int)(6*Math.random() + 1);
                   	ImageIcon icon2 = new ImageIcon("1.jpg");
                   	
                  myLabel.setIcon(icon2);
            
                   	
                   }
                    
            
                public static void main(String[] args) 
                {
                	Dice w1 = new Dice();
                	w1.setVisible(true);
                   
                }
            }
            I got error :cannot find symbol method setIcon(javax.s wing.ImageIcon)

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by kalar
              @Jos
              i make changes but i still have problem
              Code:
              import javax.swing.*;
              import java.awt.*;
              import java.awt.event.*;
              
              
              public class Dice extends JFrame implements ActionListener
              {
              	public static final int WIDTH = 200;
              	public static final int HEIGHT = 200;
                  public JFrame myLabel;
              
                  public Dice() 
                  {
                  	super();
                  	
                  	setSize(WIDTH, HEIGHT);
              	    addWindowListener(new WindowDestroyer());
              	    
                  	ImageIcon icon1 = new ImageIcon("dice8.gif");
                  	
                  	
                  	getContentPane().setLayout(new BorderLayout());
                  	JLabel  myLabel = new JLabel(icon1);
                  	getContentPane().add(myLabel, BorderLayout.NORTH);
                  	
                  	JButton button1 = new JButton("Dice");
                      button1.addActionListener(this);
                      getContentPane().add(button1, BorderLayout.SOUTH);
                  }
                      
                      public void actionPerformed(ActionEvent e)
                     {
                     	//int val = (int)(6*Math.random() + 1);
                     	ImageIcon icon2 = new ImageIcon("1.jpg");
                     	
                    myLabel.setIcon(icon2);
              
                     	
                     }
                      
              
                  public static void main(String[] args) 
                  {
                  	Dice w1 = new Dice();
                  	w1.setVisible(true);
                     
                  }
              }
              I got error :cannot find symbol method setIcon(javax.s wing.ImageIcon)
              Great; now you have two 'myLabel' variables: one is a member variable which is a
              JFrame for reasons that are beyond me. You don't even initialize it so its value
              is null. Two, you have a local variable again (just as you had before) which is a
              JLabel. It won't be visible in the actionPerformed () method for the same reasons
              as I posted before. Please think a bit before you randomly make changes to your
              code and hope that others will fix it.

              Get rid of that local variable and make that member variable a JLabel.

              kind regards,

              Jos

              Comment

              • kalar
                New Member
                • Aug 2007
                • 82

                #8
                @Jos sorry but know i am learning java swing, it is one of my first programms ( a dice). Something else i correct the code and it works but it is so strange to me to have something like that:
                Code:
                JLabel  myLabel = new JLabel(icon1);
                out of a constructor. In the book , that i have all these "new" in the examples are in a constructor.
                1)So is there a way to put it in the constructor?
                2)this program is a dice.The first picture i load is an animated gif and if i puss the button it shows another picture with the number that returns the dice.Is my thought correct about the implementation or there is a simpler way?
                Thanks in advance.

                The Correct code that it works:
                Code:
                public class Dice extends JFrame implements ActionListener
                {
                	public static final int WIDTH = 200;
                	public static final int HEIGHT = 200;
                    ImageIcon icon1 = new ImageIcon("dice8.gif");
                    JLabel  myLabel = new JLabel(icon1);
                    
                
                    public Dice() 
                    {
                    	super();
                    	
                    	setSize(WIDTH, HEIGHT);
                	    addWindowListener(new WindowDestroyer());  	
                    	
                    	getContentPane().setLayout(new BorderLayout());
                    	getContentPane().add(myLabel, BorderLayout.NORTH);
                    	
                    	JButton button1 = new JButton("Dice");
                        button1.addActionListener(this);
                        getContentPane().add(button1, BorderLayout.SOUTH);
                    }
                        
                        public void actionPerformed(ActionEvent e)
                       {
                       	//int val = (int)(6*Math.random() + 1);
                       	ImageIcon icon2 = new ImageIcon("1.jpg");
                       	
                        myLabel.setIcon(icon2);
                       //JLabel  myLabel = new JLabel(icon2);
                       //getContentPane().add(myLabel, BorderLayout.NORTH);
                       	
                       }
                        
                
                    public static void main(String[] args) 
                    {
                    	Dice w1 = new Dice();
                    	w1.setVisible(true);
                       
                    }
                }

                Comment

                Working...