Why won't the .png show up on the GUI window?

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

31 Replies - 811 Views - Last Post: 02 September 2012 - 03:58 PM Rate Topic: -----

#1 VNF  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 01-September 12

Why won't the .png show up on the GUI window?

Posted 01 September 2012 - 08:27 PM

This is a simple optional problem from the Deitel? Java book, ch. 9.8. Problem is that the .png
image will not show up on the window? I copied the code correctly from the book I believe, though
the image will not show up on the label? Thanks for help,


import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class LabelDemo {

    public static void main(String[] args) {

        JLabel northLabel = new JLabel("North");

        ImageIcon i = new ImageIcon("a.png");

        JLabel centerLabel = new JLabel(i);

        JLabel southLabel = new JLabel(i);

        southLabel.setText("South");

        JFrame application = new JFrame();

        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        application.add(northLabel, BorderLayout.NORTH);
        application.add(centerLabel, BorderLayout.CENTER);
        application.add(southLabel, BorderLayout.SOUTH);

        application.setSize(500, 500);
        application.setVisible(true);

    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Why won't the .png show up on the GUI window?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Why won't the .png show up on the GUI window?

Posted 01 September 2012 - 08:31 PM

after
ImageIcon i = new ImageIcon("a.png");
add
System.out.println(i);

will see if the a.png was correctly read
Was This Post Helpful? 0
  • +
  • -

#3 VNF  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 01-September 12

Re: Why won't the .png show up on the GUI window?

Posted 01 September 2012 - 08:36 PM

yes, a.png shows up in the output window? the a.png is a small icon, 40 pixel square. i tried using a jpg as well, and same result, no image in the GUI window?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Why won't the .png show up on the GUI window?

Posted 01 September 2012 - 08:39 PM

Yes they are displayed in a JLabel which is a JComponent with a preferredSize()
this size will be the same for .png, .jpg, .jpeg and .gif

Use a GridLayout if you want your JComponent to take all the available room
Was This Post Helpful? 0
  • +
  • -

#5 VNF  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 01-September 12

Re: Why won't the .png show up on the GUI window?

Posted 01 September 2012 - 08:41 PM

Any idea why the image will not show up in the GUI?

Thanks, V
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Why won't the .png show up on the GUI window?

Posted 01 September 2012 - 08:43 PM

It shows up. It is in your JLabel
Was This Post Helpful? 0
  • +
  • -

#7 VNF  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 01-September 12

Re: Why won't the .png show up on the GUI window?

Posted 01 September 2012 - 08:46 PM

I don't understand. The book shows the picture in the GUI window, my GUI window has no pictures in it?
Was This Post Helpful? 0
  • +
  • -

#8 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Why won't the .png show up on the GUI window?

Posted 01 September 2012 - 08:51 PM

Just to clarify: You took pbl's suggestion and printed the ImageIcon object? What was the output you got from the console? Your code looks like it should show the image unless it can't read a.png.
Was This Post Helpful? 0
  • +
  • -

#9 VNF  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 01-September 12

Re: Why won't the .png show up on the GUI window?

Posted 01 September 2012 - 08:55 PM

Yes, the output box says:
run:
a.png
BUILD SUCCESSFUL (total time: 9 seconds)

after I added the system.out.println(i) statement, though the GUI window comes up with
the labels "North" and "South" on it, though no images are displayed?

I wrote code earlier in the day that displayed these same .png images, so not sure what the
issue is?
Was This Post Helpful? 0
  • +
  • -

#10 VNF  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 01-September 12

Re: Why won't the .png show up on the GUI window?

Posted 01 September 2012 - 09:10 PM

Why would the code not be able to read a .png?

tk
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Why won't the .png show up on the GUI window?

Posted 01 September 2012 - 09:17 PM

most probably not in the same directory as the .class file

But if you tell us what the println() shows we would stop to speculate and work or the real problem
Was This Post Helpful? 0
  • +
  • -

#12 VNF  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 01-September 12

Re: Why won't the .png show up on the GUI window?

Posted 01 September 2012 - 09:20 PM

run:
a.png
BUILD SUCCESSFUL (total time: 2 seconds)


The .png is in the same class file as well... I have been trying to fix this for
four hours. ahhhhhhhhhhhhhh!
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Why won't the .png show up on the GUI window?

Posted 01 September 2012 - 09:23 PM

View PostVNF, on 02 September 2012 - 12:20 AM, said:

run:
a.png
BUILD SUCCESSFUL (total time: 2 seconds)

I can understand the println() printing a.png but who the hell is printing "BUILD SUCCESSFUL (total time: 2 seconds)"
Which IDE if ever you use ?
Was This Post Helpful? 0
  • +
  • -

#14 VNF  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 01-September 12

Re: Why won't the .png show up on the GUI window?

Posted 01 September 2012 - 09:24 PM

I'm using NetBeans 7.2 ;)
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Why won't the .png show up on the GUI window?

Posted 01 September 2012 - 09:32 PM

Sorry but for me a message like

"BUILD SUCCESSFUL (total time: 2 seconds)"
is a compilation message saying: "Yes I have succesfully compiled your program"
I really don't see how you can receive it at run time
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3