-1

This program will work perfectly and print out the JLabel if it is just a string, but if I try using an ImageIcon nothing will display.

Here is the class of evil:

public class Window extends JFrame{

    JPanel panel;
    ImageIcon imgIcon;
    JLabel label;

    public Window(String name){
        super(name);

        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationByPlatform(true);


        //addKeyListener(new KeyboardInput());

        panel = new JPanel();
        panel.setLayout(null);

        imgIcon = new ImageIcon("rorschach.jpg");
        label = new JLabel();
        label.setIcon(imgIcon);

        label.setLocation(0,0);
        label.setSize(label.getPreferredSize());



        panel.add(label);
        add(panel);

        setVisible(true);

    }

    public static void main(String[] args){
        Window window = new Window("test");
    }
}

I've also tried changing it to:

label = new JLabel(imgIcon);

but that didn't work either

On a different note, what does the line "setLocationByPlatform" do?

nicknicknick111
  • 69
  • 1
  • 2
  • 12

1 Answers1

1

Yes it is a duplicate of your last question. You were shown 3 working examples that used a null layout and dynamically changed the location of the image.

If you delete the line:

JButton left = addMotionSupport( component );

from either of the first two examples, then you have exactly what you want, a frame with a panel using a null layout with a label at location (100, 100).

In those examples, only the panel that contained the layout used a null layout. Then the code used the setSize() and setLocation() methods on the label.

In your code here you set a null layout to the panel containing the label, but you also set the frame to use a null layout, which is causing the problem.

Not only that after you set the size and location you override those values by using the setBounds() method and the width/height of the frame will be (0, 0) so the size of the label will be (0, 0) so there is nothing to paint.

Again, this was NOT done in the 3 examples. Follow the code from the examples. The code from the examples also creates the GUI components on the EDT, which is how all GUIS should be created.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Upon taking a closer look at your link, I see that the examples you were talking about were downloadable files at the bottom. I thought you were referring to the little clips of code throughout the page, my apologies and thank you – nicknicknick111 Apr 04 '15 at 16:04
  • Is there anyway that I can ask you specifically a question camickr? I revised my code to be 99% like yours (differences in dimensions) yet my jlabel still will not display. – nicknicknick111 Apr 04 '15 at 16:44
  • `differences in dimensions` - did you set the size the way I did, buy using the getPreferredSize() method? Did you use a System.out.println(...) to verify the size? Maybe the size is (0, 0) because your images was not found. I reopened the question so you can update the code with your new SSCCE, but I won't be around for a couple of hours to look at your new code. Of course your SSCCE should just be for displaying the image and not include the motion related code. – camickr Apr 04 '15 at 16:51
  • @nicknicknick111, Code works fine for me. I don't see where you display the size of the label after you set the Icon to the label. Other difference, 1) you don't create the GUI on the EDT? My example showed the proper way to create GUI components on the EDT. Why are you extending JFrame, you are not adding any functionality to the frame. Again, my example showed that you should not extend JFrame. – camickr Apr 04 '15 at 17:21
  • What do you mean by creating the GUI on the EDT? Your example involved using the graphics class to fill a rectangle, while I'm trying to draw an image (unless thats what fillRect does, correct me if I'm wrong). You have lots of code that uses the graphic class and deals with colors and size. How's that apply to drawing an image? I really don't know much on combining JLabels with the Graphics class. The only other method I've used for images is creating a custom class for each image which I think you corrected me on a while ago. (If you could link me to more example code that would be great) – nicknicknick111 Apr 05 '15 at 04:07
  • @nicknicknick111, `...creating the GUI on the EDT` Read the Swing tutorial on [Currency](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html) for more information about the `Event Dispatch Thread`. `I'm trying to draw an image` - My example shows you how to display a `JLabel` containing an `Icon`. In my example I am using an `ColorIcon`. In your case you want to use an `ImageIcon`. How the Icons paints itself is irrelevant to your code. You can change one line of code in my example to use an `ImageIcon`. If the code does not work then that means Java can't find the image. – camickr Apr 05 '15 at 04:18
  • Read the [Swing Tutorial](http://docs.oracle.com/javase/tutorial/uiswing/TOC.html) for the basics of using Swing. There are all kinds of example programs to learn from. – camickr Apr 05 '15 at 04:19