Results 1 to 10 of 10
  1. #1
    Bagzli is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default Icon wont change on a JButton

    Hi, I am trying to make my JButton change icon when its clicked, here is partial sample code that im using but it is not working and id like to know why. Could anyone please help out?

    Java Code:
    img_friend = new ImageIcon("help_friend.jpg").getImage();
    img_friendX = new ImageIcon("help_friendX.jpg").getImage();
    
    btn_image_friend.setIcon(new ImageIcon(img_friend));
    btn_image_friend.setMargin (new Insets (0, 0, 0, 0));
    btn_image_friend.setBorder (null);
    btn_image_friend.setBackground(clr_background);
    btn_image_friend.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				JOptionPane.showMessageDialog(null, "Friend Button was clicked");
    				btn_image_friend.setIcon(new ImageIcon(img_friendX));
    				repaint();
    			}
    		});
    When i press the friend button, it gives me the pop up and then it dissapears.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I can't see what you're doing wrong, and because of this I recommend that you create and post a small compilable program that demonstrates your problem, an SSCCE. If you go this route, I suggest that you use images that are readily available to us all online, and some that I've used for this purpose can be found here: Duke Images

    Here's an example of an SSCCE that uses the public domain internet images:

    Java Code:
    package yr2011.m05.c;
    
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.net.*;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class ButtonImageSwap extends JPanel {
       private static final String[] IMAGE_PATHS = {
          "http://duke.kenai.com/iconSized/duke.gif",
          "http://duke.kenai.com/iconSized/duke4.gif", 
          "http://duke.kenai.com/iconSized/dukeplug.gif"};
       private JButton imageButton = new JButton();
       private ImageIcon[] icons = new ImageIcon[IMAGE_PATHS.length];
       private int iconIndex = 0;
    
       public ButtonImageSwap() throws MalformedURLException, IOException {
          for (int i = 0; i < IMAGE_PATHS.length; i++) {
             BufferedImage bImage = ImageIO.read(new URL(IMAGE_PATHS[i]));
             icons[i] = new ImageIcon(bImage);
          }
          imageButton.setIcon(icons[iconIndex]);
          add(imageButton);
          imageButton.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                imageButtonSwapIcon();
             }
          });
       }
    
       private void imageButtonSwapIcon() {
          iconIndex++;
          iconIndex %= IMAGE_PATHS.length;
          imageButton.setIcon(icons[iconIndex]);
       }
    
       private static void createAndShowUI() {
          try {
             JFrame frame = new JFrame("ButtonImageSwap");
             frame.getContentPane().add(new ButtonImageSwap());
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.pack();
             frame.setLocationRelativeTo(null);
             frame.setVisible(true);
          } catch (HeadlessException e) {
             e.printStackTrace();
          } catch (MalformedURLException e) {
             e.printStackTrace();
          } catch (IOException e) {
             e.printStackTrace();
          }
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }
    Last edited by Fubarable; 05-15-2011 at 02:14 AM.

  3. #3
    Bagzli is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    thats the whole code, i do not know how to use images that are online. I only know how to use ones that are in the folder. If you guys can just rename couple images to fit my names it should work for you.
    Last edited by Bagzli; 05-15-2011 at 03:30 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Bagzli View Post
    [CODE]package Game;thats the whole code, i do not know how to use images that are online. I only know how to use ones that are in the folder. If you guys can just rename couple images to fit my names it should work for you.
    Others may have time to wade through the whole program, but I'm a bit pressed for time myself given my work and family obligations. If you're still stuck and without a good response in the near future, again consider creating an abbreviated version of the code, one compiles and runs, but that just has a single JButton that swaps images when pressed and none of the other unrelated code.

    Luck!

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,192
    Rep Power
    19

    Default

    Quote Originally Posted by Bagzli View Post
    thats the whole code
    You weren't asked for the whole code. You were asked to post a SSCCE. Did you go through the link?

    Setting backgrounds, borders and adding a whole bunch of JLabels are totally irrelevant to the stated problem. Good luck finding someone who's willing to wade through all that irrelevance to sort out your problem.

    If you are unwilling to put in the effort to make it as easy as possible for someone to help you, why should a volunteer have to put in extra effort to find what's wrong?

    Quote Originally Posted by Bagzli View Post
    i do not know how to use images that are online. I only know how to use ones that are in the folder.
    Are you incapable of going through Fubarable's SSCCE and learning? If so, I doubt you can be helped on a forum.

    Quote Originally Posted by Bagzli View Post
    If you guys can just rename couple images to fit my names it should work for you.
    Again, you could have put in some efort to learn by observing, and used the same images used in Fubarable's code.

    Help denied on account of laziness.

    db

  6. #6
    Bagzli is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    no need to be so mean and offensive about it, thought this would save time, most of the code is just bunch of labels and stuff being added. The problem i am having is in the lines of code from my first post, i posted whole to thing to show theres nothing more to it.

    And for the record no i am not incapable, what i posted was quite simple to understand if you were capable of reading my first post and throwing a look at rest of my code you wouldn't be raging at me like a child. I'm removing my code since you find it so offensive...

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Bagzli View Post
    no need to be so mean and offensive about it, thought this would save time, most of the code is just bunch of labels and stuff being added. The problem i am having is in the lines of code from my first post, i posted whole to thing to show theres nothing more to it.

    And for the record no i am not incapable, what i posted was quite simple to understand if you were capable of reading my first post and throwing a look at rest of my code you wouldn't be raging at me like a child. I'm removing my code since you find it so offensive...
    Regardless, my recommendation stands: If you're still stuck and without a good response in the near future, again consider creating an abbreviated version of the code, one that compiles and runs, but that just has a single JButton that swaps images when pressed and none of the other unrelated code.

  8. #8
    Bagzli is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    thanks I'll give it a try later today, I'm kind of pressed on time at the moment.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Bagzli View Post
    thanks I'll give it a try later today, I'm kind of pressed on time at the moment.
    I certainly know the feeling!

    Note that your problem could be due to not reading in the image correctly, perhaps due to looking in the wrong place for the images. Do any images show? Do you see any exceptions when you run your program? Also, it's kind of strange reading in an image as an ImageIcon only to extract the Image and then only to use it in an ImageIcon.

    Luck!

  10. #10
    Bagzli is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    found a solution on my own, I wasn't aware of .setDisabledIcon function before, using that i managed to make it work perfectly. You may close the thread.

Similar Threads

  1. icon does not change when I want it too
    By drewtrcy in forum New To Java
    Replies: 1
    Last Post: 05-05-2011, 05:10 PM
  2. Why wont my boolean variable change?
    By equal in forum New To Java
    Replies: 8
    Last Post: 02-22-2011, 10:43 PM
  3. JButton with Icon & text alignment issue
    By ShardaD in forum AWT / Swing
    Replies: 4
    Last Post: 01-11-2011, 11:10 AM
  4. Removing icon from JButton
    By 3RDofApril in forum AWT / Swing
    Replies: 3
    Last Post: 11-04-2010, 02:57 AM
  5. My icon extends my JButton
    By hitmen in forum AWT / Swing
    Replies: 5
    Last Post: 03-06-2009, 07:49 AM

Posting Permissions

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