Results 1 to 10 of 10
Thread: Icon wont change on a JButton
- 05-15-2011, 02:41 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
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(); } });
-
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 03:14 AM.
- 05-15-2011, 03:16 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
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 04:30 AM.
-
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!
- 05-15-2011, 03:30 AM #5
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?
Are you incapable of going through Fubarable's SSCCE and learning? If so, I doubt you can be helped on a forum.
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
- 05-15-2011, 04:28 AM #6
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
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.
- 05-15-2011, 04:34 AM #8
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
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!
- 05-15-2011, 11:48 PM #10
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
Similar Threads
-
icon does not change when I want it too
By drewtrcy in forum New To JavaReplies: 1Last Post: 05-05-2011, 06:10 PM -
Why wont my boolean variable change?
By equal in forum New To JavaReplies: 8Last Post: 02-22-2011, 11:43 PM -
JButton with Icon & text alignment issue
By ShardaD in forum AWT / SwingReplies: 4Last Post: 01-11-2011, 12:10 PM -
Removing icon from JButton
By 3RDofApril in forum AWT / SwingReplies: 3Last Post: 11-04-2010, 03:57 AM -
My icon extends my JButton
By hitmen in forum AWT / SwingReplies: 5Last Post: 03-06-2009, 08:49 AM
Bookmarks