Results 1 to 19 of 19
Like Tree1Likes
  • 1 Post By Norm

Thread: Using images in JLabel and/or JPanel

  1. #1
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    84
    Rep Power
    0

    Default Using images in JLabel and/or JPanel

    I read many posts online and watched tutorials on YouTube. I can't seem to get this work. Notice I am not using any drawing/graphics capabilities - I'd like to keep it this way as we have not gotten that far yet in my studies.

    The image is in a source folder titled Images under/in my project.

    What am I doing wrong?


    Java Code:
    	private JPanel jpImage = new JPanel();
    	private JLabel lblImage;
    	private ImageIcon image;
            private Image img;
    
    // All those above defined prior to method
    // Within method (relevant to code above) ...
    
    		img = new ImageIcon(this.getClass().getResource("/relax.jpg")).getImage();
    		jpImage.setBounds(325, 71, 124, 131);
    		image = new ImageIcon(img);
    		lblImage.setIcon(image); // NullPointerException
    		jpImage.add(lblImage);
    		contentPane.add(jpImage);

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default Re: Using images in JLabel and/or JPanel

    What variable has the null value when that statement is executed?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    84
    Rep Power
    0

    Default Re: Using images in JLabel and/or JPanel

    The beginning of the Stack Trace looks like this ...

    Java Code:
    java.lang.NullPointerException
    	at edu.witc.TrainStation.view.TrainStation.initComponents(TrainStation.java:129)
    	at edu.witc.TrainStation.view.TrainStation.<init>(TrainStation.java:65)
    	at edu.witc.TrainStation.view.StartProgram$1.run(StartProgram.java:24)
    129 is the
    Java Code:
     lblImage.setIcon(image)
    originally stated.
    65 is in my public method for the class when calling
    Java Code:
     initComponents()
    which is the method that builds the GUI
    24 is in my main class Start Program when I instantiate the JFrame
    Java Code:
    TrainStation frame = new TrainStation();
    When I comment out everything from img = newIcon & contentPane.add(jpImage) the program complies and runs.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default Re: Using images in JLabel and/or JPanel

    What variable has the null value when line 129 is executed? Add a println() statement just before line 129 to print out the values of all the variables so you can see which one is null.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    84
    Rep Power
    0

    Default Re: Using images in JLabel and/or JPanel

    Quote Originally Posted by Norm View Post
    What variable has the null value when line 129 is executed? Add a println() statement just before line 129 to print out the values of all the variables so you can see which one is null.
    It prints out

    sun.awt.image.ToolkitImage@409046f8
    javax.swing.ImageIcon@402364e5

    Then goes into the exception shown above.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,786
    Rep Power
    5

    Default Re: Using images in JLabel and/or JPanel

    Where do you create your JLabel?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    84
    Rep Power
    0

    Default Re: Using images in JLabel and/or JPanel

    Quote Originally Posted by jim829 View Post
    Where do you create your JLabel?

    Regards,
    Jim
    The label (and all my components) are created directly under the main class.

    Java Code:
    public class TrainStation extends JFrame { 
    
    // All labels, panels, images, buttons & the like are created as private objects. Later defined in the method that builds GUI
    
    	private JPanel jpImage = new JPanel();
    	private JLabel lblImage;
    	private ImageIcon image;
    	private Image img;
    
    // ... everything else
    
    
    	public TrainStation() {
    		setResizable(false);
    		initComponents(); // Build GUI
    		setModel(); // For JList
    		
    		@SuppressWarnings("unused")
    		MainController controller = new MainController(this.getView(), kiosk.getView(), invoice.getView());
    		
    	} // End of method
    
    ....
    
    	public void initComponents() {
    ....
            img = new ImageIcon(this.getClass().getResource("/relax.jpg")).getImage();
    		jpImage.setBounds(325, 71, 124, 131);
    		image = new ImageIcon(img);
    		System.out.println(img);
    		System.out.println(image);
    		lblImage.setIcon(image);
    		jpImage.add(lblImage);
    		contentPane.add(jpImage);
    
    }

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,786
    Rep Power
    5

    Default Re: Using images in JLabel and/or JPanel

    I still don't see it. I see where you declared the JLabel but I don't see where you did a new JLabel() construct.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default Re: Using images in JLabel and/or JPanel

    sun.awt.image.ToolkitImage@409046f8
    javax.swing.ImageIcon@402364e5
    What variables are those the values of? Can you post the code that printed those values?
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    84
    Rep Power
    0

    Default Re: Using images in JLabel and/or JPanel

    Quote Originally Posted by jim829 View Post
    I still don't see it. I see where you declared the JLabel but I don't see where you did a new JLabel() construct.

    Regards,
    Jim
    Nailed it!! Thank you.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default Re: Using images in JLabel and/or JPanel

    Will you be able to find the problem next time?
    Or will you have to come back here and have someone else find the problem for you?
    Programming is about solving problems. You need to learn how to do it your self.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    84
    Rep Power
    0

    Default Re: Using images in JLabel and/or JPanel

    Quote Originally Posted by Norm View Post
    Will you be able to find the problem next time?
    Or will you have to come back here and have someone else find the problem for you?
    Programming is about solving problems. You need to learn how to do it your self.
    Norm,

    I put a great amount of effort into my work and only ask for help when I need another pair of eyes. Sometimes you miss things, even the obvious, after hours of staring at code - especially once frustration sets in. I've never posted anything asking for fully written code, always for advice on a specific topic. This is how you learn and get better. You have every right to state your opinion, and you have helped me a few times in the past. Know that I am appreciate of that and I thank you for your time and knowledge.

    Will I repeat this mistake in the future? I cannot definitively say no (no one can), however it is unlikely.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default Re: Using images in JLabel and/or JPanel

    I wasn't meaning that you were asking someone else to do your work.
    What happened though is that some one else did do your work. It's not your fault that another person found the problem and pointed it out. My approach was to show you a technique that you could use to find the problem. However that was bypassed by someone else coming in and doing it. Most experienced programmers can look at your code and see the problem in seconds. But if we just give Ops the fix for a problem, I don't think the OPs would learn problem solving techniques.
    gimbal2 likes this.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,105
    Rep Power
    6

    Default Re: Using images in JLabel and/or JPanel

    Quote Originally Posted by Norm View Post
    Most experienced programmers can look at your code and see the problem in seconds. But if we just give Ops the fix for a problem, I don't think the OPs would learn problem solving techniques.
    Not doubting your dedication to want to help. But perhaps this should be reasoned as a parent. Sometimes the hand needs to be held, and sometimes you need to let go and just let the kids discover stuff for themselves.

    Nobody had to tell me how to find simple problems, that realization eventually grew on its own after being stumped many times in a row. That frustration the OP speaks of can be a blocker, but there is going to come a moment where it is going to be a step up to a higher realization so to speak.

    So I ask the question: is it always a good idea to boost someone up to that level? Or might that moment be forced to soon?

    My point being that it is near impossible to tell how far someone is based on the minimal information that tends to be given in a forum post... perhaps we should be more careful with how helpful we actually are.

    This question made me realize a second thing: sometimes its just best to butt-out of a thread if someone has already engaged into guiding the thread to a specific conclusion.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  15. #15
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,786
    Rep Power
    5

    Default Re: Using images in JLabel and/or JPanel

    Quote Originally Posted by gimbal2 View Post
    This question made me realize a second thing: sometimes its just best to butt-out of a thread if someone has already engaged into guiding the thread to a specific conclusion.
    Well, I actually welcome folks jumping in if I am helping someone. In spite of my experience in programming, I miss things.
    Or someone may know a better way to do it. So helping someone even though I may be missing the crux of the problem
    would just clutter the forum with useless posts. My technique for helping folks has been to carefully watch how the majority
    of the primary contributors and moderators jump in and help and to emulate that as well as helping folks the way I was helped
    when I was in their situation. This also includes providing examples from time to time to explain how some aspect of Java is used.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default Re: Using images in JLabel and/or JPanel

    The old proverb:
    When a man is hungry you can
    give him a fish and feed him for a day
    teach him to fish and feed him for life.

    Giving a solution without showing how it was reached does not teach problem solving.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,105
    Rep Power
    6

    Default Re: Using images in JLabel and/or JPanel

    I agree with you, I really do. I'm being a little philosophical thinking about if it is ALWAYS a good idea to try and teach someone how to fish. Perhaps it is only a good idea if the person you teach has at least already bought the pole.

    That's my thing to ponder about for the weekend, I shouldn't bother you guys with that. Sorry.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  18. #18
    mallorz is offline Member
    Join Date
    Jan 2014
    Posts
    84
    Rep Power
    0

    Default Re: Using images in JLabel and/or JPanel

    Norm - Understood. You lost me a bit on your last questions before discovering the answer. I am willing to continue on to learn the method you were trying to teach me.

    I didn't mean to start anything. I truthfully do get "writers-block" if you will from pure frustration of an on-going issue, regardless of how utterly stupid/small it is. I agree with gimbal that typically after stumbling on things 2-3 times it generally sticks, often times even after the first. I feel I should also add that I am new to Java and I try my best to research on my own before posting to a forum.
    Last edited by mallorz; 09-26-2014 at 07:24 PM.

  19. #19
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Using images in JLabel and/or JPanel

    Quote Originally Posted by Norm View Post
    The old proverb:
    When a man is hungry you can
    give him a fish and feed him for a day
    teach him to fish and ...
    ... and he'll spend Sunday mornings downing beer!
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Last JLabel I add to JPanel doesnt show up
    By Ambergahill in forum New To Java
    Replies: 6
    Last Post: 01-29-2014, 09:07 PM
  2. [Urgent Help] JLabel images display
    By baobao816 in forum AWT / Swing
    Replies: 2
    Last Post: 05-12-2012, 10:46 AM
  3. Replies: 0
    Last Post: 12-16-2010, 06:14 PM
  4. How to resize images to fit JLabel?
    By Krooger in forum New To Java
    Replies: 3
    Last Post: 11-24-2010, 02:22 PM
  5. Adding a JLabel to a JPanel - jlabel not showing
    By Bongeh in forum New To Java
    Replies: 17
    Last Post: 04-07-2010, 12:02 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •