Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: How to display a button with an image on it?

  1. #1
    Member ice's Avatar
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default How to display a button with an image on it?

    Hello guys

    Below code mean to display a button with an image on it, the image is real in my computer, but it only displays a blank button. Do you know why?

    Many thanks

    package brainydraw;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class PictureButton extends JFrame
    {
     
        public PictureButton()
        {
            setSize(200, 200);
            setLocation(200, 200);
     
            Icon icon = new ImageIcon("Poppy.Jpg");
            JButton button = new JButton(icon);
            button.addActionListener(new ActionListener()
            {
     
                public void actionPerformed(ActionEvent ae)
                {
                    System.out.println("guess what?");
                }
            });
     
            Container content = getContentPane();
            content.setLayout(new FlowLayout());
            content.add(button);
        }
     
        public static void main(String[] args)
        {
            JFrame f = new PictureButton();
            f.addWindowListener(new WindowAdapter()
            {
     
                public void windowClosing(WindowEvent we)
                {
                    System.exit(0);
                }
            });
            f.setVisible(true);
      }
    }


  2. #2
    Member
    Join Date
    Oct 2010
    Posts
    40
    Thanks
    0
    Thanked 2 Times in 1 Post

    Default Re: How to display a button with an image on it?

    try this:

    Icon icon = new ImageIcon(getClass().getResource("Poppy.jpg"));

  3. #3
    Member ice's Avatar
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to display a button with an image on it?

    Quote Originally Posted by relixus View Post
    try this:

    Icon icon = new ImageIcon(getClass().getResource("Poppy.jpg"));
    Hi relixus

    Thank you very much for your advice, I tried to replace "Icon icon = new ImageIcon("Poppy.jpg")" with yours "Icon icon = new ImageIcon(getClass().getResource("Poppy.jpg"))", then ran it again, now it is not even showing the frame with the blank button?
    Any more thought?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to display a button with an image on it?

    The JVM most likely cannot find the image because it isn't where you said it is. In your first code listing, try using the full path to the image.

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How to display a button with an image on it?

    Do you have to do getClass().getResource(Image title)?

    Why not just

    ImageIcon icon = new ImageIcon("Poppy.jpg");

    ?

    As long as the file is in the same directory as your program.

    at worst, why not

    ImageIcon icon = new ImageIcon(code project path +"/" +"images/Poppy.jpg");

    or even just

    ImageIcon icon = new ImageIcon(code project path +"/" +"Poppy.jpg");

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to display a button with an image on it?

    Quote Originally Posted by javapenguin View Post
    Do you have to do getClass().getResource(Image title)?

    Why not just...

    ImageIcon icon = new ImageIcon("Poppy.jpg");
    Try packaging your application and running it on a different computer. Using getResource allows you to package the app and images into a single jar. Relying on full paths is not very portable from one computer to the next, and in many cases will break immediately.

  7. The Following User Says Thank You to copeg For This Useful Post:

    javapenguin (November 12th, 2010)

  8. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Question Re: How to display a button with an image on it?

    Quote Originally Posted by copeg View Post
    Try packaging your application and running it on a different computer. Using getResource allows you to package the app and images into a single jar. Relying on full paths is not very portable from one computer to the next, and in many cases will break immediately.
    So how would I get it to work on both my computer and another computer? Would I have to import a jar file?


  9. #8
    Member
    Join Date
    Oct 2010
    Posts
    40
    Thanks
    0
    Thanked 2 Times in 1 Post

    Default Re: How to display a button with an image on it?

    when you compile your project into a jar file the image for your button will be included inside the jar file, and you will have problems referencing to it directly, u have to use getClass().getResource() to use those resources inside your jar file.

  10. #9
    Member
    Join Date
    Oct 2010
    Posts
    40
    Thanks
    0
    Thanked 2 Times in 1 Post

    Default Re: How to display a button with an image on it?

    Quote Originally Posted by ice View Post
    Hi relixus

    Thank you very much for your advice, I tried to replace "Icon icon = new ImageIcon("Poppy.jpg")" with yours "Icon icon = new ImageIcon(getClass().getResource("Poppy.jpg"))", then ran it again, now it is not even showing the frame with the blank button?
    Any more thought?
    is poppy.jpg in the same directory as your java file? u should put them in the same folder.

  11. #10
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

  12. #11
    Member ice's Avatar
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    60
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to display a button with an image on it?

    Thank you very much all of you, I presumed that the location of image file didn't matter..., now it displays all good.

Similar Threads

  1. Display in JOptionPane
    By t-rank in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 19th, 2010, 12:09 AM
  2. Using JFileChooser to open and display an image
    By JavaN0ob in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 31st, 2010, 06:01 PM
  3. Display file name
    By Puk284 in forum Java Servlet
    Replies: 1
    Last Post: April 13th, 2010, 03:13 AM
  4. display time
    By kalees in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: January 1st, 2010, 07:40 AM
  5. Pixel Alteration in an image/image rotation
    By bguy in forum Java Theory & Questions
    Replies: 3
    Last Post: November 1st, 2009, 10:50 AM