Results 1 to 14 of 14
Like Tree2Likes
  • 1 Post By camickr
  • 1 Post By littledrummerboy

Thread: Making this JLabel Appear

  1. #1
    BradR is offline Member
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

    Default Making this JLabel Appear

    My friends and are are trying to program a smaller version of the pokemon safari zone. I'm attempting to output a small, 7x6 image of a grassy area, but this isn't outputting an image, can someone please tell me what I am doing wrong?

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.util.*;
    /**
     *Grid Component
     */
    public class Grid extends JFrame
    {
        private JLabel mapLabel;
        public Grid()
        {
            super("Safari Zone");
            setSize(300,300);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel imagePanel = new JPanel();
            add(imagePanel);
            ImageIcon zone = new ImageIcon("C:\\Documents and Settings\\Bradley\\My Documents\\BradleyWork2010\\AP Java\\ClassWork\\Final\\Map.jpeg");
            mapLabel = new JLabel(zone);
            imagePanel.add(mapLabel);
            setVisible(true);
    
        }
    }
    Last edited by Fubarable; 06-12-2011 at 12:31 AM. Reason: code tags added

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    I would guess the image isn't found.

    Also make sure you create the GUI on the Event Dispatch Thread. See: Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)

  3. #3
    Join Date
    May 2011
    Location
    Maryland
    Posts
    38
    Rep Power
    0

    Default

    maybe try using .validate() or .repaint() on the JLabel?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    Quote Originally Posted by littledrummerboy View Post
    maybe try using .validate() or .repaint() on the JLabel?
    Not required. Irrelevant.

    db

  5. #5
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    After the setVisible(true) line add:

    imagePanel.repaint();
    imagePanel.updateUI();
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Why not imagePanel.validate() as well? It can't do any harm. The OP could also consider sacrificing a small goat.

    Or he or she could consider whether or not the image as specified was found. And create the GUI on the Event Dispatch Thread. This involves putting some faith in what camickr says about things Swing: but it's a sure winner for the goats.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by FlyNn View Post
    After the setVisible(true) line add:

    imagePanel.repaint();
    imagePanel.updateUI();
    Just to be clear, this will not help at all, and you don't call updateUI with Swing. Please re-read with camickr, Darryl and more recently pbrockway have posted.

  8. #8
    Join Date
    May 2011
    Location
    Maryland
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by DarrylBurke View Post
    Not required. Irrelevant.

    db
    how come it's irrelevant? i recently did a project where I had to work with tons of jlabels and jpanels and i often found when they weren't showing up it was because I needed to update them with one of those methods so I thought maybe that'd help him too.

  9. #9
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    how come it's irrelevant?
    Because this is the initial display of the GUI and you don't need to do anything like that.

    repaint() is when you've changed a property of a visible component.

    updateUI() is when you change the LAF of a visible component. You should never use this method. See How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel) for the proper way to change the LAF on a visible GUI.

    i often found when they weren't showing up it was because I needed to update them with one of those methods so I thought maybe that'd help him too.
    Its a brute force solution that some people use because they don't understand how Swing works and should NOT be used or recommended. The proper way to tell Swing that components have been added is to use:

    Java Code:
    panel.add(...);
    panel.revalidate();
    panel.repaint();
    Fubarable likes this.

  10. #10
    BradR is offline Member
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

    Default

    I changed the image from JPEG to PNG and it worked, just wondering why this happened

  11. #11
    Join Date
    May 2011
    Location
    Maryland
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post
    Its a brute force solution that some people use because they don't understand how Swing works and should NOT be used or recommended. The proper way to tell Swing that components have been added is to use:
    Thank you, but could you explain why it should not be used? It's true I don't understand swing very well, so since it fixed my problems i was unaware that it wasn't the best option.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by BradR View Post
    I changed the image from JPEG to PNG and it worked, just wondering why this happened
    Perhaps because the file is a PNG file and not a JPG?

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by littledrummerboy View Post
    Thank you, but could you explain why it should not be used? It's true I don't understand swing very well, so since it fixed my problems i was unaware that it wasn't the best option.
    I thought camickr already explained this. What about his explanation do you not understand?

    Some additional thoughts:

    Quote Originally Posted by littledrummerboy View Post
    maybe try using .validate() or .repaint() on the JLabel?
    Here you're advocating calling validate or repaint on the label. Since the label isn't acting as a container, validate makes no sense, and since if you set it's text or image it will display it automatically, repaint is completely unnecessary.

    Quote Originally Posted by littledrummerboy View Post
    how come it's irrelevant? i recently did a project where I had to work with tons of jlabels and jpanels and i often found when they weren't showing up it was because I needed to update them with one of those methods so I thought maybe that'd help him too.
    Then you had a bug in that code that you don't yet understand (and neither do we as we don't have access to your code).

    And since you admittedly don't understand Swing which is pretty darn complex, perhaps you might want to study up on Swing before giving advice so that your understand and advice are both a bit better. The Swing tutorials are a good place to start and the source code would be the next place to have a look.
    Last edited by Fubarable; 06-13-2011 at 02:41 AM.

  14. #14
    Join Date
    May 2011
    Location
    Maryland
    Posts
    38
    Rep Power
    0

    Default

    well he just said why it shouldn't be used in this case but he seemed very adament about not using it at all and so i was just wondering why that was

    And since you admittedly don't understand Swing which is pretty darn complex, perhaps you might want to study up on Swing before giving advice so that your understand and advice are both a bit better. The Swing tutorials are a good place to start and the source code would be the next place to have a look.
    I'm sorry, i was just trying to give some advice since I knew that helped me, didn't realize it would do any harm
    i'll definitely start reading up on swing though, thank you!
    DarrylBurke likes this.

Similar Threads

  1. Adding a JLabel to a JPanel - jlabel not showing
    By Bongeh in forum New To Java
    Replies: 17
    Last Post: 04-06-2010, 11:02 PM
  2. jLabel
    By Matty in forum AWT / Swing
    Replies: 3
    Last Post: 09-22-2008, 10:22 PM
  3. GUI - JLabel
    By Azndaddy in forum New To Java
    Replies: 8
    Last Post: 05-02-2008, 07:03 AM
  4. JLabel
    By Jack in forum AWT / Swing
    Replies: 2
    Last Post: 07-02-2007, 01:55 PM
  5. JLabel
    By Freddie in forum AWT / Swing
    Replies: 2
    Last Post: 05-29-2007, 02:19 PM

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
  •