×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    50

    Changing the Image on a button

    Our app uses LWUIT for Series 40.

    We are trying to change the image on a button when it is clicked. We do this elsewhere with the same code and it works fine, but in this case the image will not change. I have tried commenting out on the styling lines, but this does not make a difference. Does it have something to do with the call being in an ActionListener?

    Code:
    southCon = new Container();
    
    southCon.setLayout(new BoxLayout(BoxLayout.X_AXIS));
    
    southCon.getStyle().setMargin(Component.LEFT, 10);
    
    southCon.getStyle().setMargin(Component.TOP, 10);
    
    southCon.getStyle().setMargin(Component.BOTTOM, 0);
    
    southCon.getStyle().setPadding(0, 0, 0, 0);
    
    
    
    likeImageLabel = new Button();
    
    if(m.isLiked()){
    
    likeImageLabel.setIcon(likeImagePressed);
    
    }else{
    
    likeImageLabel.setIcon(likeImage);
    
    }
    
    
                likeImageLabel.setTickerEnabled(false);
    
                //likeImageLabel.setPressedIcon(likeImagePressed);
    
                likeImageLabel.setUnselectedStyle(unlStyle);
    
                likeImageLabel.setPressedStyle(unlStyle);
    
                likeImageLabel.getStyle().setFgColor(0xa5a1b2);
    
                likeImageLabel.getPressedStyle().setFgColor(0xa5a1b2);
    
                likeImageLabel.setWidth(MINI_ICON_SIZE);
    
                likeImageLabel.setHeight(MINI_ICON_SIZE);
    
                likeImageLabel.setPreferredH(MINI_ICON_SIZE);
    
                likeImageLabel.setPreferredW(MINI_ICON_SIZE);
    
                likeImageLabel.getStyle().setMargin(Component.LEFT, 0);
    
                likeImageLabel.getStyle().setMargin(Component.RIGHT, 5);
    
                likeImageLabel.getStyle().setMargin(Component.TOP, 0);
    
                likeImageLabel.getStyle().setPadding(0, 0, 0, 0);
    
                likeImageLabel.getPressedStyle().setMargin(Component.LEFT, 0);
    
                likeImageLabel.getPressedStyle().setMargin(Component.RIGHT, 5);
    
                likeImageLabel.getPressedStyle().setMargin(Component.TOP, 0);
    
                likeImageLabel.getPressedStyle().setPadding(0, 0, 0, 0);
    
                likeImageLabel.setSelectedStyle(likeImageLabel.getStyle());
    
                likeImageLabel.setDisabledStyle(likeImageLabel.getStyle());
    
                if(!isCachedFeed) {
    
                    likeImageLabel.addActionListener(new ActionListener() {
    
                        public void actionPerformed(ActionEvent actionEvent) {
    
                        Log.v(""+m.user_has_liked);
    
                           if(m.isLiked()){
    
                        	   InstaMedia.unlike(m.mediaId);
    
                        	   m.toggleLike();
    
                           
    
                        	   likeImageLabel.setIcon(likeImage);
    
                          Log.v("Set icon");
    
                           
    
                           }else{
    
                        	   InstaMedia.like(m.mediaId);
    
                        	   m.toggleLike();
    
                 
    
                         
    
                        	   likeImageLabel.setIcon(likeImagePressed); 
    
                        	   Log.v("Set icon pressed");
    
                           }
    
                        
    
                           likeImageLabel.repaint();
    
                           southCon.revalidate();
    
                        }
    
                    });
    
                }
    
                else {
    
                    likeImageLabel.setEnabled(false);
    
                }
    
    southCon.addComponent(likeImageLabel);

  2. #2
    Registered User
    Join Date
    Dec 2012
    Location
    India
    Posts
    264

    Re: Changing the Image on a button

    I am surprised why this is not working.Try repainting the form itself.

    Also double check the image initialization and the image itself.I often make mistake while copy-pasting image initialization code and end up opening the wrong image.

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    50

    Re: Changing the Image on a button

    Thank you for your response. I am now repainting and revalidating all the way up the chain...but still it won't change...

    Code:
                           likeImageLabel.repaint();
                           southCon.revalidate();
                           southCon.repaint();
                           cellCon.revalidate();
                           cellCon.repaint();
                           listContainer.revalidate();
                           listContainer.repaint();
                           thisForm.revalidate();
                           thisForm.repaint();

  4. #4
    Registered User
    Join Date
    Mar 2013
    Location
    Nasik, India
    Posts
    21

    Re: Changing the Image on a button

    Have you tried below methods. (They works for me in case i need to change the button image after it clicks)

    likeImageLabel .setRolloverIcon(imgNewImage);
    likeImageLabel .setPressedIcon(imgNewImage);

    Thanks,
    Vishal Bharati.

Similar Threads

  1. how to fit image on button if image size is large than button
    By vikranttanwar@ in forum Mobile Java UI and User Experience
    Replies: 5
    Last Post: 2013-08-14, 00:34
  2. How to change image on button on another button click
    By Manpreet_Singh in forum Mobile Java UI and User Experience
    Replies: 2
    Last Post: 2013-08-07, 13:05
  3. Changing power on/off button
    By Swiv in forum General Development Questions
    Replies: 4
    Last Post: 2006-07-27, 23:41
  4. Run-time changing CBA button's name
    By Nokia_Archive in forum Symbian User Interface
    Replies: 1
    Last Post: 2002-05-30, 12:30

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •