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 7 of 7

Thread: [SWT-App] How to point to a directory full of images globally (for each computer).

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default [SWT-App] How to point to a directory full of images globally (for each computer).

    Hey Java'ners,

    Basically I am coding using the SWT Widget Java library, using eclipse, and I've already tried to google my problem but with no luck. I'm creating an application and in the code it points to various pictures on my computer, so it looks more visually appealing. So here lies the problem, I have no clue how to point to a directory full of images which I have, to each computer that runs the app.

    So here, the code is just pointing to one image on my Desktop;
    Image imageFile= new Image(display, "C://Users//PC-name//Desktop//blah.png");

    I thought maybe I put in "...Desktop//blah.png" it could still find the image, but no, IOException error, can't find image.
    Does anyone know how to help me ?

    Thank you.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: [SWT-App] How to point to a directory full of images globally (for each computer)

    how to point to a directory full of images which I have, to each computer that runs the app.
    Will all the PCs have the images in the same local directory? If you have a hardcoded path in your program then all the PCs will have to have the same directory structure for the images.

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [SWT-App] How to point to a directory full of images globally (for each computer)

    Yeah, I'm going to Zip up my .jar runnable file with the images inside a directory/file. So They'll download the whole bundle. I wouldn't have a clue how to hard-code the path. Wouldn't know where to look, or start. You'll need to elaborate please.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: [SWT-App] How to point to a directory full of images globally (for each computer)

    This code is an example of a "hardcoded" path.

    Image imageFile= new Image(display, "C://Users//PC-name//Desktop//blah.png");

    Hardcoded means the use of a literal instead of computing a value, the value is "hardcoded" in the statement
    Last edited by Norm; August 31st, 2011 at 04:30 PM.

  5. #5
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: [SWT-App] How to point to a directory full of images globally (for each computer)

    Not sure how to do it with SWT, but images are normally bundled with a Java application as resources in a jar file:
    Creating a JAR File (The Java™ Tutorials > Deployment > Packaging Programs in JAR Files)
    This page also briefly covers loading images using getResource():
    How to Use Icons (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

  6. #6
    Junior Member
    Join Date
    Aug 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [SWT-App] How to point to a directory full of images globally (for each computer)

    Yeah, I see what your saying, but not everyone will have C drive as there local disk, or "PC-name" as there PC name. Which brings me back to my first point.

    EDIT: Thanks for the links, I'll read them tommorow and will get back to you guys by than, see how it goes.

  7. #7
    Junior Member
    Join Date
    Aug 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [SWT-App] How to point to a directory full of images globally (for each computer)

    I believe I've got it, I have to create an executable JAR file and run it on another computer to truly see if it works, but basically those links didn't really help. Though you said "getResource()". So Googled it for SWT, surely enough found it.

    Image image = new Image(display, <ClassName>.class.getResourceAsStream("bg.png"));

    And what I did is dragged and dropped the images I wanted inside of the PACKAGE folder where my Java class is. So I'm guessing if I create an executable JAR file, it will carry those images as well. So awesome, if it works, thanks.

    EDIT: Sorry double post

Similar Threads

  1. [SOLVED] Update Values Globally
    By techwiz24 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 10th, 2011, 06:20 PM
  2. Full Window when run the program.
    By faysal40 in forum AWT / Java Swing
    Replies: 1
    Last Post: May 12th, 2011, 11:37 PM
  3. Insert master or gloabal java script to disable globally onclick
    By karnesb in forum Java Theory & Questions
    Replies: 2
    Last Post: October 12th, 2008, 04:10 PM