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 14 of 14

Thread: Image in Java will not display

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Image in Java will not display

    Hello I'm not getting any errors but for some reason my image won't display. Also the picture is a png image. Here is my code that should display the image:

    ImageIcon icon = new ImageIcon("pictures/J Pipe's Card.png");

    imageLabel = new JLabel ( icon );

    add ( imageLabel );


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Image in Java will not display

    Is the image file in the location where the program is looking for it?
    Create a new File("pictures/J Pipe's Card.png") object and print out its absolute path to see where the program is looking for the file.

    What container is the add() method in? Is that container being displayed?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Image in Java will not display

    Quote Originally Posted by Norm View Post
    Is the image file in the location where the program is looking for it?
    Create a new File("pictures/J Pipe's Card.png") object and print out its absolute path to see where the program is looking for the file.

    What container is the add() method in? Is that container being displayed?
    I think it is an addActionListener but I'm not sure.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Image in Java will not display

    What printed for the location of the image? Was it the correct path?

    What class is the add() method in?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Image in Java will not display

    Quote Originally Posted by Norm View Post
    What printed for the location of the image? Was it the correct path?

    What class is the add() method in?
    I'm not sure but it appears nothing displays and no error. Here is some of my code which includes the lines to display the image, which it doesn't:

    ImageIcon icon = new ImageIcon("J Pipe's Card.png");

    imageLabel = new JLabel ( icon );
    heightLabel = new JLabel ("Height: \n\t");
    widthLabel = new JLabel ( "Width: \n\t");
    areaLabel = new JLabel ( "Area: \n\t");
    costLabel = new JLabel ( "Cost: \n\t");

    ButtonListener listener = new ButtonListener();
    fmt = NumberFormat.getCurrencyInstance();
    height = new JTextField (5);
    width = new JTextField (5);

    calculate = new JButton("Calculate");
    calculate.addActionListener( new ButtonListener());
    // cost.addActionListener ( new PaymentListener());
    //num.addActionListener ( new PaymentListener());
    //rate.addActionListener ( new PaymentListener());

    // add ( imageLabel );
    add ( calculate );

    add (heightLabel);

    add (height);

    add (widthLabel);

    add (width);

    add (areaLabel);

    add (costLabel);

    setPreferredSize ( new Dimension( 500, 250 ));

    setBackground ( Color.cyan );
    }

    --- Update ---

    Also I have a ButtonListener class and an ActionPerformed. Here is the code for those:

    private class ButtonListener implements ActionListener
    {
    //
    // Updates the monthly payment when the button is pushed.
    //

    public void actionPerformed (ActionEvent event)
    {
    double h, w, a, c;
    h = Double.parseDouble(height.getText());
    w = Double.parseDouble(width.getText());
    a = h * w;
    c = a * aRate;

    areaLabel.setText("Area is: " + a );
    costLabel.setText("Cost is: " + fmt.format(c) );

    }
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Image in Java will not display

    Where is the File object I suggested and the print out of the path to make sure the path was right?

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Image in Java will not display

    Quote Originally Posted by Norm View Post
    Where is the File object I suggested and the print out of the path to make sure the path was right?

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    I'm not sure what you mean by the File object. The image is a png file and it is in My Pictures. I'm not sure where on my laptop the My Pictures is located, such as the C:/ drive.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Image in Java will not display

    I'm not sure what you mean by the File object.
    See post #2.

    The print out will show you the path to where the program is looking for the image file.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Image in Java will not display

    Quote Originally Posted by Norm View Post
    See post #2.

    The print out will show you the path to where the program is looking for the image file.
    I tried to run the debugger to see but I can't copy and paste from the debugger. It just says for ImageIcon icon = <object reference> for local variables if that helps any.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Image in Java will not display

    Is the program finding the image in the place it is looking?
    To see where the program is looking, do what I suggested in post#2
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Image in Java will not display

    This is the exact path, however it gives an error illegal escape character. Is it impossible or something to display an image in Java? I remember I had a project in my Java programming class that involved displaying an image and that didn't work either and I followed the instructions for loading and displaying an image according to the book.

    ImageIcon icon = new ImageIcon("C:\Users\Public\Pictures\J Pipe's Card.png");

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Image in Java will not display

    Try using forward slashes instead of those.

    Yes it is possible to display an image in java.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Image in Java will not display

    Quote Originally Posted by Norm View Post
    Try using forward slashes instead of those.

    Yes it is possible to display an image in java.
    I'm still not sure what is going on. I tried / instead of \ but it still doesn't show the image. Does Java not allow someone to display images made in paint or anything with a png extenstion?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Image in Java will not display

    First: You have to read the image file to be able to display its contents.
    Then you need to do the right thing in the code to display the image.
    Are you doing the right thing in your code so the image will be displayed? Hard to say without having a version of the code that can be compiled and executed for testing.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Why won't this image display?
    By mkrage in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 3rd, 2012, 09:05 PM
  2. Image Doesn't Display with Qualified Image Path????
    By swaginator in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 31st, 2012, 12:29 AM
  3. Can't display an image...
    By barebackingtiger in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 25th, 2012, 07:21 PM
  4. Display Image (png)
    By vimalaranjan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 3rd, 2011, 06:44 PM
  5. How to display a button with an image on it?
    By ice in forum AWT / Java Swing
    Replies: 10
    Last Post: November 13th, 2010, 06:20 AM

Tags for this Thread