Java swing: trouble understanding adding image to JLabel





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I hope everyone is doing alright this fine day.



I'm learning swing and I was confused by how to reference an image. I understand that I should use a JLabel and then add that JLabel to the Frame using this.add();, but even looking at the oracle documentation here:



https://docs.oracle.com/javase/6/docs/api/javax/swing/ImageIcon.html



It is still unclear how to reference a file without giving the entire path like



C:UserssomeUsereclipse-workspaceandSoOn.png



And I can't do that. I have to send my work to my teacher once I'm done, and the code won't reference the file like it does on my system. I tried several things, and I ended up making a new folder in the src in eclipse called ImageAssets and moving the files there, but nothing seems to work. Here is what it looks like



I added a folder called ImageAssets



Here is an example of my attempt to display an image from within the package.



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

public class Hangman extends JFrame
{
JButton playGameButton,
OptionsButton;
private ImageIcon hangman7;
private JLabel mainLabel;

public static void main(String args)
{
new Hangman();
}

public Hangman()
{
this.setSize(1000,800);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Hangman");
this.setResizable(false);
playGameButton = new JButton("Start Game");
OptionsButton = new JButton("Options");

//hangman7 = new ImageIcon(getClass().getResource("Images\ hangman7.png"));//just an attempt at something
mainLabel = new JLabel();
mainLabel.setIcon(new ImageIcon("hangman7.png"));


JPanel somePanel = new JPanel();
somePanel.setLayout(new BorderLayout());
somePanel.add(playGameButton, BorderLayout.WEST);
somePanel.add(OptionsButton, BorderLayout.EAST);
somePanel.add(mainLabel, BorderLayout.CENTER);

this.add(somePanel);
this.validate();
}


Thank you so much for taking the time to help me. I tried to be very detailed; if anything is unclear please ask.










share|improve this question































    1















    I hope everyone is doing alright this fine day.



    I'm learning swing and I was confused by how to reference an image. I understand that I should use a JLabel and then add that JLabel to the Frame using this.add();, but even looking at the oracle documentation here:



    https://docs.oracle.com/javase/6/docs/api/javax/swing/ImageIcon.html



    It is still unclear how to reference a file without giving the entire path like



    C:UserssomeUsereclipse-workspaceandSoOn.png



    And I can't do that. I have to send my work to my teacher once I'm done, and the code won't reference the file like it does on my system. I tried several things, and I ended up making a new folder in the src in eclipse called ImageAssets and moving the files there, but nothing seems to work. Here is what it looks like



    I added a folder called ImageAssets



    Here is an example of my attempt to display an image from within the package.



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

    public class Hangman extends JFrame
    {
    JButton playGameButton,
    OptionsButton;
    private ImageIcon hangman7;
    private JLabel mainLabel;

    public static void main(String args)
    {
    new Hangman();
    }

    public Hangman()
    {
    this.setSize(1000,800);
    this.setLocationRelativeTo(null);
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setTitle("Hangman");
    this.setResizable(false);
    playGameButton = new JButton("Start Game");
    OptionsButton = new JButton("Options");

    //hangman7 = new ImageIcon(getClass().getResource("Images\ hangman7.png"));//just an attempt at something
    mainLabel = new JLabel();
    mainLabel.setIcon(new ImageIcon("hangman7.png"));


    JPanel somePanel = new JPanel();
    somePanel.setLayout(new BorderLayout());
    somePanel.add(playGameButton, BorderLayout.WEST);
    somePanel.add(OptionsButton, BorderLayout.EAST);
    somePanel.add(mainLabel, BorderLayout.CENTER);

    this.add(somePanel);
    this.validate();
    }


    Thank you so much for taking the time to help me. I tried to be very detailed; if anything is unclear please ask.










    share|improve this question



























      1












      1








      1








      I hope everyone is doing alright this fine day.



      I'm learning swing and I was confused by how to reference an image. I understand that I should use a JLabel and then add that JLabel to the Frame using this.add();, but even looking at the oracle documentation here:



      https://docs.oracle.com/javase/6/docs/api/javax/swing/ImageIcon.html



      It is still unclear how to reference a file without giving the entire path like



      C:UserssomeUsereclipse-workspaceandSoOn.png



      And I can't do that. I have to send my work to my teacher once I'm done, and the code won't reference the file like it does on my system. I tried several things, and I ended up making a new folder in the src in eclipse called ImageAssets and moving the files there, but nothing seems to work. Here is what it looks like



      I added a folder called ImageAssets



      Here is an example of my attempt to display an image from within the package.



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

      public class Hangman extends JFrame
      {
      JButton playGameButton,
      OptionsButton;
      private ImageIcon hangman7;
      private JLabel mainLabel;

      public static void main(String args)
      {
      new Hangman();
      }

      public Hangman()
      {
      this.setSize(1000,800);
      this.setLocationRelativeTo(null);
      this.setVisible(true);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.setTitle("Hangman");
      this.setResizable(false);
      playGameButton = new JButton("Start Game");
      OptionsButton = new JButton("Options");

      //hangman7 = new ImageIcon(getClass().getResource("Images\ hangman7.png"));//just an attempt at something
      mainLabel = new JLabel();
      mainLabel.setIcon(new ImageIcon("hangman7.png"));


      JPanel somePanel = new JPanel();
      somePanel.setLayout(new BorderLayout());
      somePanel.add(playGameButton, BorderLayout.WEST);
      somePanel.add(OptionsButton, BorderLayout.EAST);
      somePanel.add(mainLabel, BorderLayout.CENTER);

      this.add(somePanel);
      this.validate();
      }


      Thank you so much for taking the time to help me. I tried to be very detailed; if anything is unclear please ask.










      share|improve this question
















      I hope everyone is doing alright this fine day.



      I'm learning swing and I was confused by how to reference an image. I understand that I should use a JLabel and then add that JLabel to the Frame using this.add();, but even looking at the oracle documentation here:



      https://docs.oracle.com/javase/6/docs/api/javax/swing/ImageIcon.html



      It is still unclear how to reference a file without giving the entire path like



      C:UserssomeUsereclipse-workspaceandSoOn.png



      And I can't do that. I have to send my work to my teacher once I'm done, and the code won't reference the file like it does on my system. I tried several things, and I ended up making a new folder in the src in eclipse called ImageAssets and moving the files there, but nothing seems to work. Here is what it looks like



      I added a folder called ImageAssets



      Here is an example of my attempt to display an image from within the package.



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

      public class Hangman extends JFrame
      {
      JButton playGameButton,
      OptionsButton;
      private ImageIcon hangman7;
      private JLabel mainLabel;

      public static void main(String args)
      {
      new Hangman();
      }

      public Hangman()
      {
      this.setSize(1000,800);
      this.setLocationRelativeTo(null);
      this.setVisible(true);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.setTitle("Hangman");
      this.setResizable(false);
      playGameButton = new JButton("Start Game");
      OptionsButton = new JButton("Options");

      //hangman7 = new ImageIcon(getClass().getResource("Images\ hangman7.png"));//just an attempt at something
      mainLabel = new JLabel();
      mainLabel.setIcon(new ImageIcon("hangman7.png"));


      JPanel somePanel = new JPanel();
      somePanel.setLayout(new BorderLayout());
      somePanel.add(playGameButton, BorderLayout.WEST);
      somePanel.add(OptionsButton, BorderLayout.EAST);
      somePanel.add(mainLabel, BorderLayout.CENTER);

      this.add(somePanel);
      this.validate();
      }


      Thank you so much for taking the time to help me. I tried to be very detailed; if anything is unclear please ask.







      java eclipse swing embedded-resource






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 17 '18 at 7:41









      Andrew Thompson

      154k29166352




      154k29166352










      asked Nov 17 '18 at 7:18









      Terry DorseyTerry Dorsey

      150113




      150113
























          1 Answer
          1






          active

          oldest

          votes


















          5














          In your case, you want let the class loader find the resource, like this:



          mainLabel.setIcon(
          new ImageIcon(getClass().getResource("/ImageAssets/hangman7.png")));





          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%2f53349106%2fjava-swing-trouble-understanding-adding-image-to-jlabel%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









            5














            In your case, you want let the class loader find the resource, like this:



            mainLabel.setIcon(
            new ImageIcon(getClass().getResource("/ImageAssets/hangman7.png")));





            share|improve this answer




























              5














              In your case, you want let the class loader find the resource, like this:



              mainLabel.setIcon(
              new ImageIcon(getClass().getResource("/ImageAssets/hangman7.png")));





              share|improve this answer


























                5












                5








                5







                In your case, you want let the class loader find the resource, like this:



                mainLabel.setIcon(
                new ImageIcon(getClass().getResource("/ImageAssets/hangman7.png")));





                share|improve this answer













                In your case, you want let the class loader find the resource, like this:



                mainLabel.setIcon(
                new ImageIcon(getClass().getResource("/ImageAssets/hangman7.png")));






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 17 '18 at 7:36









                Joseph FitzgeraldJoseph Fitzgerald

                1613




                1613
































                    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%2f53349106%2fjava-swing-trouble-understanding-adding-image-to-jlabel%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

                    Bressuire

                    Vorschmack

                    Quarantine