Results 1 to 12 of 12
  1. #1
    corbokhan is offline Member
    Join Date
    Oct 2010
    Posts
    16
    Rep Power
    0

    Default Image On a JLabel

    I've been trying to get a local image (image.png) to show up on a label. Currently, the label will show up (have changed the background color so I can tell) but the image will not show.

    Here's the source code for the particular part:


    ImageIcon logo = new ImageIcon("spinitlogo.png");
    JLabel imageLabel = new JLabel(logo);
    imageLabel.setBounds(540,25,500,304);
    imageLabel.setOpaque(true);

    add(imageLabel) //imageLabel is added to JPanel constructor

    The label shows up, because I can see the color change, I just cannot see the actual image. Any recommendations?

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

    Default

    Are you looking for the image file where you think you're looking? Have you checked to see if it's in the user.dir?

    Consider creating an SSCCE (please see my signature link below on this).

  3. #3
    corbokhan is offline Member
    Join Date
    Oct 2010
    Posts
    16
    Rep Power
    0

    Default

    The file is placed in the user directory, so I feel like it should be working. Does this code look correct at first glance, or is it clear that my image location in wrong.

    I feel like the example I gave is pretty close to an SSCCE, so not sure what other example I could give.

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

    Default

    Quote Originally Posted by corbokhan View Post
    The file is placed in the user directory, so I feel like it should be working. Does this code look correct at first glance, or is it clear that my image location in wrong.
    How do you know this? Have you printed out the System properties for user.dir to know that the file is in the right location?

    I feel like the example I gave is pretty close to an SSCCE, so not sure what other example I could give.
    Can I run the code and test it for myself to see your problem? If not, it is not "very close to an SSCCE", since that's the whole point of an SSCCE.

  5. #5
    corbokhan is offline Member
    Join Date
    Oct 2010
    Posts
    16
    Rep Power
    0

    Default

    I've taken everything out that seems irrelevant and highlighted my problem spot in red. The JLabel shows up in the frame, but the picture does not. What do you think?

    Java Code:
    public class NewInterface extends JFrame {
    	
    	public static void main(String[] args) {
    			
    		// ------ Frame ------
    		JFrame window = new JFrame("Spin It! Article Spinner");
    			window.setSize(1065,740);
    			window.setLocation(200,50);
    			window.setContentPane(new ContentPanel());
    			window.setJMenuBar(menuBar);
    			window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			window.setVisible(true);
    	} // end main
    	
    	public void init() {
    		setContentPane(new ContentPanel());
    	}
    	
    	
    	/*
    	 *  ---------- Nested Classes ----------
    	 */
    	
    	
    	public static class ContentPanel extends JPanel implements ActionListener {
    						
    		ContentPanel() {
    			
    			setLayout(null);
    			setBackground(Color.LIGHT_GRAY);
    						
    			//Below components are declared outside of class
    			[COLOR="Red"]image = new ImageIcon("/images/spinitlogo.png");
    	
    			imageLabel = new JLabel(image);
    				imageLabel.setBounds(540,25,500,304);
    				imageLabel.setOpaque(true);[/COLOR]
    				
    			//Below components are declared outside of class
    			directionsLabel = new JLabel("Text:");
    			directionsLabel1 = new JLabel("Text:");
    			directionsLabel2 = new JLabel("Text:");
    				
    			//Below components are declared outside of class
    			titleText = new JTextField();
    			titleSpun = new JTextField();
    			bodyText = new JTextPane();	
    			spunText = new JTextPane();
    			sigText = new JTextPane();
    			sigSpun = new JTextPane();
    			directions = new JTextPane();
    			wordCount = new JTextField();
    				
    				
    			//Below components are declared outside of class
    			spinIt = new JButton("Spin It!");
    			resetButton = new JButton("Reset");
    			saveButton = new JButton("Save");
    	
    			
    			//Below components are declared outside of class
    			bodyScroll = new JScrollPane(bodyText);
    			spunScroll = new JScrollPane(spunText);
    			sigScroll = new JScrollPane(sigText);
    			sigSpunScroll = new JScrollPane(sigSpun);
    			directionsScroll = new JScrollPane(directions);
    				
    								
    			// ------ Add To ContentPanel ------	
    			add(JTextPanes)
    			add(JTextFields)
    			add(JButtons)
    			
    			[COLOR="red"]add(imageLabel);		[/COLOR]			
    		}
    		
    } // End
    Last edited by Fubarable; 11-05-2010 at 12:15 AM. Reason: Moderator Edit: Code tags added

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

    Default

    I can't run any of this, so I have little to say. Please read the link in my signature about how to create an SSCCE. It must compile and run on someone else's computer.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    Use getImageLoadStatus() and see what value it has.
    It should be 8 if its found the image and loaded it.

  8. #8
    corbokhan is offline Member
    Join Date
    Oct 2010
    Posts
    16
    Rep Power
    0

    Default

    Alright, here's some code which should be sufficient to compile. Thanks again for the help.

    PHP Code:
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    
    
    public class ImageTest extends JFrame {
    
    	static ImageIcon image;
    	static JLabel imageLabel;
    	
    	public static void main(String[] args) {
    		
    		JFrame window = new JFrame("Image Test");
    			window.setSize(1065,740);
    			window.setLocation(200,50);
    			window.setContentPane(new ContentPanel());
    			window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			window.setVisible(true);
    	}
    	
    	public void init() {
    		setContentPane(new ContentPanel());
    	}
    	
    	public static class ContentPanel extends JPanel {
    						
    		ContentPanel() {
    			
    			setLayout(null);
    			setBackground(Color.LIGHT_GRAY);
    			
    			image = new ImageIcon("spinitlogo.psd");
    			
    			imageLabel = new JLabel(image);
    				imageLabel.setBounds(540,25,500,304);
    				imageLabel.setOpaque(true);	
    			
    			add(imageLabel);
    			
    						
    		}
    	
    	}
    	
    } // End NewInterface Class

  9. #9
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    304
    Rep Power
    6

    Default

    In your project, where is the class located and where is the Image located?

    Add a debug line printing out the System.getProperty("user.dir"), above where you are getting your image.

    Can you get it to work with a JPG image?

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

    Default

    Quote Originally Posted by StormyWaters View Post
    In your project, where is the class located and where is the Image located?

    Add a debug line printing out the System.getProperty("user.dir"), above where you are getting your image.

    Can you get it to work with a JPG image?
    Exactly. That's what I asked him in my first two posts here, whether the file is correctly located relative to the user.dir, and he assured me that it was, but who knows. Especially since the code he has posted works with known images:
    Java Code:
    import java.awt.Color;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    public class ImageTest extends JFrame {
       public static final String IMAGE_PATH = "http://duke.kenai.com/" + "Oracle/OracleStratSmall.png";
    
       static ImageIcon image;
       static JLabel imageLabel;
    
       public static void main(String[] args) {
    
          JFrame window = new JFrame("Image Test");
          window.setSize(1065, 740);
          window.setLocation(200, 50);
          window.setContentPane(new ContentPanel());
          window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          window.setVisible(true);
       }
    
       public static class ContentPanel extends JPanel {
    
          ContentPanel() {
    
             setLayout(null);
             setBackground(Color.LIGHT_GRAY);
    
             //!! image = new ImageIcon("spinitlogo.psd");
             try {
                URL imageUrl = new URL(IMAGE_PATH);
                BufferedImage bImage = ImageIO.read(imageUrl);
                image = new ImageIcon(bImage);
             } catch (MalformedURLException e) {
                e.printStackTrace();
                System.exit(-1);
             } catch (IOException e) {
                e.printStackTrace();
                System.exit(-1);
             }
    
             imageLabel = new JLabel(image);
             imageLabel.setBounds(540, 25, 500, 304);
             imageLabel.setOpaque(true);
    
             add(imageLabel);
    
          }
    
       }
    
    }
    Last edited by Fubarable; 11-05-2010 at 11:05 PM.

  11. #11
    corbokhan is offline Member
    Join Date
    Oct 2010
    Posts
    16
    Rep Power
    0

    Default

    Great I got it working! I really appreciate all the help, sorry that was so difficult for me!

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

Similar Threads

  1. Replies: 13
    Last Post: 09-29-2010, 10:55 PM
  2. JLabel wont appear in front of background image
    By Andolph23 in forum AWT / Swing
    Replies: 5
    Last Post: 05-27-2010, 01:42 AM
  3. Image in JLabel
    By ishere11 in forum AWT / Swing
    Replies: 2
    Last Post: 04-29-2010, 08:02 AM
  4. 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
  5. Replies: 0
    Last Post: 01-08-2008, 05:29 PM

Posting Permissions

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