Need help, ImageIcon on the Label

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • crystal2005
    New Member
    • Apr 2007
    • 44

    Need help, ImageIcon on the Label

    Hi all,

    Can anyone tell me why my image doesn't appear in my frame's container? When I change imageIcon to just normal Label, it does go work.

    In another condition, if i don't use method return (what is it called? emm...). ImageIcon does work on my normal Label.

    Code:
    package practise;
    
    import java.awt.*;
    import javax.swing.*;
    
    public class layout_test {
    	
    	public Container createContentPane() {
    		JPanel contentPane = new JPanel(new BorderLayout());
    		contentPane.setOpaque(true);
    		
    		contentPane.add(createNorthPanel(), BorderLayout.NORTH);
    		contentPane.add(createCenterPanel(), BorderLayout.CENTER);
    		
    		return contentPane;
    	}
    	
    	public JPanel createCenterPanel() {
    		JPanel centerPanel = new JPanel();
    		JLabel image = new JLabel(new ImageIcon("Bovinetine.jpg")); // Image doesn't appear
    		centerPanel.add(image);
    		return centerPanel;
    	}
    	
    	public JPanel createNorthPanel() {
    		JPanel northPanel = new JPanel(new GridLayout(1,3));
    		JButton b1 = new JButton("b1"), b2 = new JButton("b2"), b3 = new JButton("b3");
    		northPanel.add(b1);
    		northPanel.add(b2);
    		northPanel.add(b3);
    		return northPanel;
    	}
    	
    	public static void main(String[] args) {
    		JFrame frame = new JFrame("Testing Layout with Image");
    		layout_test layout = new layout_test();
    		frame.setContentPane(layout.createContentPane());
    		frame.pack();
    		frame.setVisible(true);
    	}
    
    }
    Thanks for any advice...
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    The most common reason for an image not appearing is that it doesn't exist, you misspelled the filename or the file is in the wrong place.

    Comment

    Working...