Not displaying image jframe












0















I've been trying to make an image appear, but nothing happens. I've looked up various methods and applied them, yet I haven't been able to achieve it. I'm starting to think that maybe I'm placing in the wrong file. In applets, I only need to place the image in the build folder. As for JFrame, I'm not sure where. I tried placing it in build, no image. I tried placing it in src, still nothing. Below is a bit of what I've been testing so far. Maybe it's the image location or the code.



Could someone tell me what am I doing wrong?



import javax.swing.*;
import java.awt.*;

public class Changey extends javax.swing.JFrame {
JPanel panel = (JPanel) this.getContentPane();
//JLabel label = new JLabel();

public Changey() {
initComponents();
panel.setLayout(null);

//ImageIcon icon = new ImageIcon("Cookie.png");
//JLabel label = new JLabel(icon);
//panel.add(label);

//ImageIcon icon = new ImageIcon(getClass().getResource("Cookie.png"));

//Icon icon = new ImageIcon("Cookie.png");
//JLabel label = new JLabel( icon );
//frame.add(label);

//label.setIcon(new ImageIcon("Cookie.png"));
//panel.add(label);

//JLabel img = new JLabel(new ImageIcon("Cookie.png"));
//img.setBounds(200, 300, 100, 100); // x, y, width, height
//panel.add(img);

//ImageIcon image = new ImageIcon("Cookie.png");
//label.setBounds(10, 10, 400, 400);
//panel.add(label);
//image.getImage();

//label.setIcon(new ImageIcon("Cookie.PNG"));
//label.setBounds(10, 10, 400, 400);
//panel.add(label);

setSize(1130,380);
setTitle("Image");
}









share|improve this question

























  • 1) Why the bunch of commented out code? For better help sooner, edit to add a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example that tries to load a single image. 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or combinations of them along with layout padding and borders for white space. 3) Application resources ..

    – Andrew Thompson
    Nov 20 '18 at 23:16











  • .. will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An embedded-resource must be accessed by URL rather than file. See the info. page for embedded resource for how to form the URL.

    – Andrew Thompson
    Nov 20 '18 at 23:16
















0















I've been trying to make an image appear, but nothing happens. I've looked up various methods and applied them, yet I haven't been able to achieve it. I'm starting to think that maybe I'm placing in the wrong file. In applets, I only need to place the image in the build folder. As for JFrame, I'm not sure where. I tried placing it in build, no image. I tried placing it in src, still nothing. Below is a bit of what I've been testing so far. Maybe it's the image location or the code.



Could someone tell me what am I doing wrong?



import javax.swing.*;
import java.awt.*;

public class Changey extends javax.swing.JFrame {
JPanel panel = (JPanel) this.getContentPane();
//JLabel label = new JLabel();

public Changey() {
initComponents();
panel.setLayout(null);

//ImageIcon icon = new ImageIcon("Cookie.png");
//JLabel label = new JLabel(icon);
//panel.add(label);

//ImageIcon icon = new ImageIcon(getClass().getResource("Cookie.png"));

//Icon icon = new ImageIcon("Cookie.png");
//JLabel label = new JLabel( icon );
//frame.add(label);

//label.setIcon(new ImageIcon("Cookie.png"));
//panel.add(label);

//JLabel img = new JLabel(new ImageIcon("Cookie.png"));
//img.setBounds(200, 300, 100, 100); // x, y, width, height
//panel.add(img);

//ImageIcon image = new ImageIcon("Cookie.png");
//label.setBounds(10, 10, 400, 400);
//panel.add(label);
//image.getImage();

//label.setIcon(new ImageIcon("Cookie.PNG"));
//label.setBounds(10, 10, 400, 400);
//panel.add(label);

setSize(1130,380);
setTitle("Image");
}









share|improve this question

























  • 1) Why the bunch of commented out code? For better help sooner, edit to add a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example that tries to load a single image. 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or combinations of them along with layout padding and borders for white space. 3) Application resources ..

    – Andrew Thompson
    Nov 20 '18 at 23:16











  • .. will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An embedded-resource must be accessed by URL rather than file. See the info. page for embedded resource for how to form the URL.

    – Andrew Thompson
    Nov 20 '18 at 23:16














0












0








0








I've been trying to make an image appear, but nothing happens. I've looked up various methods and applied them, yet I haven't been able to achieve it. I'm starting to think that maybe I'm placing in the wrong file. In applets, I only need to place the image in the build folder. As for JFrame, I'm not sure where. I tried placing it in build, no image. I tried placing it in src, still nothing. Below is a bit of what I've been testing so far. Maybe it's the image location or the code.



Could someone tell me what am I doing wrong?



import javax.swing.*;
import java.awt.*;

public class Changey extends javax.swing.JFrame {
JPanel panel = (JPanel) this.getContentPane();
//JLabel label = new JLabel();

public Changey() {
initComponents();
panel.setLayout(null);

//ImageIcon icon = new ImageIcon("Cookie.png");
//JLabel label = new JLabel(icon);
//panel.add(label);

//ImageIcon icon = new ImageIcon(getClass().getResource("Cookie.png"));

//Icon icon = new ImageIcon("Cookie.png");
//JLabel label = new JLabel( icon );
//frame.add(label);

//label.setIcon(new ImageIcon("Cookie.png"));
//panel.add(label);

//JLabel img = new JLabel(new ImageIcon("Cookie.png"));
//img.setBounds(200, 300, 100, 100); // x, y, width, height
//panel.add(img);

//ImageIcon image = new ImageIcon("Cookie.png");
//label.setBounds(10, 10, 400, 400);
//panel.add(label);
//image.getImage();

//label.setIcon(new ImageIcon("Cookie.PNG"));
//label.setBounds(10, 10, 400, 400);
//panel.add(label);

setSize(1130,380);
setTitle("Image");
}









share|improve this question
















I've been trying to make an image appear, but nothing happens. I've looked up various methods and applied them, yet I haven't been able to achieve it. I'm starting to think that maybe I'm placing in the wrong file. In applets, I only need to place the image in the build folder. As for JFrame, I'm not sure where. I tried placing it in build, no image. I tried placing it in src, still nothing. Below is a bit of what I've been testing so far. Maybe it's the image location or the code.



Could someone tell me what am I doing wrong?



import javax.swing.*;
import java.awt.*;

public class Changey extends javax.swing.JFrame {
JPanel panel = (JPanel) this.getContentPane();
//JLabel label = new JLabel();

public Changey() {
initComponents();
panel.setLayout(null);

//ImageIcon icon = new ImageIcon("Cookie.png");
//JLabel label = new JLabel(icon);
//panel.add(label);

//ImageIcon icon = new ImageIcon(getClass().getResource("Cookie.png"));

//Icon icon = new ImageIcon("Cookie.png");
//JLabel label = new JLabel( icon );
//frame.add(label);

//label.setIcon(new ImageIcon("Cookie.png"));
//panel.add(label);

//JLabel img = new JLabel(new ImageIcon("Cookie.png"));
//img.setBounds(200, 300, 100, 100); // x, y, width, height
//panel.add(img);

//ImageIcon image = new ImageIcon("Cookie.png");
//label.setBounds(10, 10, 400, 400);
//panel.add(label);
//image.getImage();

//label.setIcon(new ImageIcon("Cookie.PNG"));
//label.setBounds(10, 10, 400, 400);
//panel.add(label);

setSize(1130,380);
setTitle("Image");
}






java image swing jframe embedded-resource






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 23:14









Andrew Thompson

153k28162345




153k28162345










asked Nov 20 '18 at 22:26









DigiLeiDigiLei

23




23













  • 1) Why the bunch of commented out code? For better help sooner, edit to add a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example that tries to load a single image. 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or combinations of them along with layout padding and borders for white space. 3) Application resources ..

    – Andrew Thompson
    Nov 20 '18 at 23:16











  • .. will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An embedded-resource must be accessed by URL rather than file. See the info. page for embedded resource for how to form the URL.

    – Andrew Thompson
    Nov 20 '18 at 23:16



















  • 1) Why the bunch of commented out code? For better help sooner, edit to add a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example that tries to load a single image. 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or combinations of them along with layout padding and borders for white space. 3) Application resources ..

    – Andrew Thompson
    Nov 20 '18 at 23:16











  • .. will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An embedded-resource must be accessed by URL rather than file. See the info. page for embedded resource for how to form the URL.

    – Andrew Thompson
    Nov 20 '18 at 23:16

















1) Why the bunch of commented out code? For better help sooner, edit to add a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example that tries to load a single image. 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or combinations of them along with layout padding and borders for white space. 3) Application resources ..

– Andrew Thompson
Nov 20 '18 at 23:16





1) Why the bunch of commented out code? For better help sooner, edit to add a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example that tries to load a single image. 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or combinations of them along with layout padding and borders for white space. 3) Application resources ..

– Andrew Thompson
Nov 20 '18 at 23:16













.. will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An embedded-resource must be accessed by URL rather than file. See the info. page for embedded resource for how to form the URL.

– Andrew Thompson
Nov 20 '18 at 23:16





.. will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An embedded-resource must be accessed by URL rather than file. See the info. page for embedded resource for how to form the URL.

– Andrew Thompson
Nov 20 '18 at 23:16












1 Answer
1






active

oldest

votes


















0














After the ImageIcon() constructor you can call image.getImageLoadStatus() to see how the loading went.



To check if the code can see a file, use
File myFile = new File("Cookie.png");
System.out.println(myFile.exists());



If you use the file as a resource, it should be in the classpath.



If you use the file with a relative path, it should be relative to the application's start folder.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402521%2fnot-displaying-image-jframe%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    After the ImageIcon() constructor you can call image.getImageLoadStatus() to see how the loading went.



    To check if the code can see a file, use
    File myFile = new File("Cookie.png");
    System.out.println(myFile.exists());



    If you use the file as a resource, it should be in the classpath.



    If you use the file with a relative path, it should be relative to the application's start folder.






    share|improve this answer




























      0














      After the ImageIcon() constructor you can call image.getImageLoadStatus() to see how the loading went.



      To check if the code can see a file, use
      File myFile = new File("Cookie.png");
      System.out.println(myFile.exists());



      If you use the file as a resource, it should be in the classpath.



      If you use the file with a relative path, it should be relative to the application's start folder.






      share|improve this answer


























        0












        0








        0







        After the ImageIcon() constructor you can call image.getImageLoadStatus() to see how the loading went.



        To check if the code can see a file, use
        File myFile = new File("Cookie.png");
        System.out.println(myFile.exists());



        If you use the file as a resource, it should be in the classpath.



        If you use the file with a relative path, it should be relative to the application's start folder.






        share|improve this answer













        After the ImageIcon() constructor you can call image.getImageLoadStatus() to see how the loading went.



        To check if the code can see a file, use
        File myFile = new File("Cookie.png");
        System.out.println(myFile.exists());



        If you use the file as a resource, it should be in the classpath.



        If you use the file with a relative path, it should be relative to the application's start folder.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 0:23









        Perdi EstaquelPerdi Estaquel

        6701519




        6701519
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402521%2fnot-displaying-image-jframe%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to change which sound is reproduced for terminal bell?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

            Can I use Tabulator js library in my java Spring + Thymeleaf project?