Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Image Doesn't Display with Qualified Image Path????

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    6
    My Mood
    Mellow
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry Image Doesn't Display with Qualified Image Path????

    I'm new to java, I started about 3 weeks ago. But I was making a program just for practice that lets you make HTML somewhat easy. The program isn't very useful but it gave me something to code. The only problem I was having was; whenever I ran the program the images didn't appear unless I put the full system path all the way from the drive. I wouldn't mind this but if I try running the program on another computer I have to go back and edit the code which isn't very cool when your trying to impress your parents. Well if anyone could help that would be awesome. Thanx in advance


    My code:




    import java.awt.BorderLayout;
    import java.awt.Graphics;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    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.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;


    public class StartingScreen extends JFrame {


    String path = "F:/My Programs/Java/CreateHTML/EasyHTML/src/resources/Computing4Every1Logo.jpg";
    String easyPath = "F:/My Programs/Java/CreateHTML/EasyHTML/src/resources/Easy_HTML_Logo.png";
    ImageIcon img = new ImageIcon(path);
    ImageIcon easyImg = new ImageIcon(easyPath);
    int x = 0;
    int y = 20;
    int width = 30;
    int height = 40;
    Graphics g;
    private static final long serialVersionUID = -7765334663035669163L;


    public StartingScreen() {

    super("image");
    setSize(700,500);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    JPanel top = new JPanel(new GridBagLayout());
    JPanel middle = new JPanel(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.insets = new Insets(15,15,15,15);

    JLabel label = new JLabel(img);
    JLabel presents = new JLabel("Presents:");
    JLabel label2 = new JLabel(easyImg);

    gbc.gridx = 0;
    gbc.gridy = 50;
    top.add(presents, gbc);
    gbc.gridx = 0;
    gbc.gridy = 0;
    top.add(label, gbc);
    middle.add(label2, gbc);

    add(middle, BorderLayout.CENTER);
    add(top, BorderLayout.NORTH);


    //menubar
    JMenuBar j = new JMenuBar();
    JMenu start = new JMenu("Start");
    JMenu newHTML = new JMenu("New HTML");
    JMenuItem menuScreen = new JMenuItem("Menu Screen");
    JMenuItem insImg = new JMenuItem("Insert Image");
    JMenuItem insTxt = new JMenuItem("Insert Text");
    setJMenuBar(j);
    j.add(start);
    start.add(menuScreen);
    start.add(newHTML);
    newHTML.add(insImg);
    newHTML.add(insTxt);


    menuScreen.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    Menu a = new Menu();
    a.setVisible(true);
    dispose();
    }
    });




    }

    }


  2. #2
    Junior Member
    Join Date
    Mar 2012
    Posts
    6
    My Mood
    Mellow
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Image Doesn't Display with Qualified Image Path????

    I probably should have mentioned that I am using Eclipse.

  3. #3
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: Image Doesn't Display with Qualified Image Path????

    I think its kind of a bad idea to reference an image from a local drive..

    Simply copy the image file to your eclipse project main directory and refrence it as image.jpg. Either build the image inside the package when exporting or simply add it to the folder when youre giving the program out of using on another computer..

  4. #4
    Junior Member
    Join Date
    Mar 2012
    Posts
    6
    My Mood
    Mellow
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Image Doesn't Display with Qualified Image Path????

    I tried putting it into the 'bin' folder but it wouldn't display unless I put the full path including the local drive.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Image Doesn't Display with Qualified Image Path????

    If you put the class files and the image in a jar file and use the classloader's getResource method you will not have to worry about moving or losing the image file. It will always be there in the jar file.
    Do a search here on the forum for code samples that use getResource.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Mar 2012
    Posts
    6
    My Mood
    Mellow
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Image Doesn't Display with Qualified Image Path????

    Okay thank you.

Similar Threads

  1. Can't display an image...
    By barebackingtiger in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 25th, 2012, 07:21 PM
  2. Trouble getting an image to display.
    By Skyhigh32 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 23rd, 2011, 07:52 AM
  3. Display Image (png)
    By vimalaranjan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 3rd, 2011, 06:44 PM
  4. How to display a button with an image on it?
    By ice in forum AWT / Java Swing
    Replies: 10
    Last Post: November 13th, 2010, 06:20 AM
  5. Using JFileChooser to open and display an image
    By JavaN0ob in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 31st, 2010, 06:01 PM