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

Thread: Open location from JList

  1. #1
    Member hexwind's Avatar
    Join Date
    May 2011
    Location
    Tunisia
    Posts
    48
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Open location from JList

    Hey everyone

    I have a JList that contain items (file paths). I want each item in the list to take me to that particular path in my computer.
    For example, if i have an item that has "C:\Program Files" I want it to open a window with that location.
    I don't know if that is possible or not. If not, is there any way to copy and paste the location from the list? how to add stuff to the system clipboard ?

    thanks
    My website : MediDev


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Open location from JList

    I want it to open a window with that location.
    What do you want to see in the window. A list of files and folders that you can select and open?
    Or do you want to open an instance of JFileChooser?

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

    Default Re: Open location from JList

    Good luck with that. You're looking for some kind of 'Java Desktop Integration'. I think there have been projects, but all were unloved children who eventually withered away and died.

    Do you need your application to be portable? If you don't, then your best bet is probably to try to use java.lang.ProcessBuilder to launch a native file browser and pass it your file path as an argument so that it can open at the right place. If you'd like to attempt something with copy-and-paste, java.awt.Robot is your tool of choice. I would advise against java.awt.Robot - it's usually ok for quick jobs in very tightly constrained environments, but it's too easy to 'break' the connection between your Java app and the native apps you expect it to be communicating with.

  4. #4
    Member hexwind's Avatar
    Join Date
    May 2011
    Location
    Tunisia
    Posts
    48
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Open location from JList

    Quote Originally Posted by Norm View Post
    What do you want to see in the window. A list of files and folders that you can select and open?
    Or do you want to open an instance of JFileChooser?
    Quote Originally Posted by Sean4u View Post
    Do you need your application to be portable? If you don't, then your best bet is probably to try to use java.lang.ProcessBuilder to launch a native file browser and pass it your file path as an argument so that it can open at the right place.
    This.
    I want to open a file browser. just like going on Start>My Documents for example. know what I mean?
    My website : MediDev

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

    Default Re: Open location from JList

    I want to open a file browser.
    On Windows would that be the Windows Explorer?

  6. #6
    Member hexwind's Avatar
    Join Date
    May 2011
    Location
    Tunisia
    Posts
    48
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Open location from JList

    Quote Originally Posted by Norm View Post
    On Windows would that be the Windows Explorer?
    Exactly (sorry English ain't my native language lol)
    My website : MediDev

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Open location from JList

    Windows Explorer is a program that comes with MS Windows. It is THE major program that I use to look at folders and files on windows. Here is the commandline to start it on my system:
    C:\WINDOWS\explorer.exe /n,/e,C:\

  8. #8
    Member hexwind's Avatar
    Join Date
    May 2011
    Location
    Tunisia
    Posts
    48
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Open location from JList

    Great, but how to start it once I double click on an item from my JList? and not only open it, but open it with a particular location
    My website : MediDev

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Open location from JList

    See the Process, Runtime and ProcessBuilder classes for ways to execute OS commands.
    Build the command line you want to execute and pass it to the class of your choice.
    On the commandline I posted, the program opens at the path at the end of the command line. Here is another example:
    C:\WINDOWS\explorer.exe /n,/e,D:\JavaDevelopment\Testing\ForumQuestions6
    For testing:
    Create a shortcut on your desktop and change the Target: field to the above command with the path changed to a valid path on your PC.
    Change the Start In: field to C:\Windows
    Last edited by Norm; August 21st, 2011 at 01:15 PM.

  10. #10
    Member hexwind's Avatar
    Join Date
    May 2011
    Location
    Tunisia
    Posts
    48
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Open location from JList

    Quote Originally Posted by Norm View Post
    See the Process, Runtime and ProcessBuilder classes for ways to execute OS commands.
    Build the command line you want to execute and pass it to the class of your choice.
    On the commandline I posted, the program opens at the path at the end of the command line. Here is another example:
    C:\WINDOWS\explorer.exe /n,/e,D:\JavaDevelopment\Testing\ForumQuestions6
    For testing:
    Create a shortcut on your desktop and change the Target: field to the above command with the path changed to a valid path on your PC.
    Change the Start In: field to C:\Windows

    I tried this on a standalone program.
    here is my code
    import java.io.*;
    public class TestRun {
    	public static void main(String[] args) throws IOException {
    		Runtime r = Runtime.getRuntime();
    		r.exec("C:\\WINDOWS\\explorer.exe /n,/e,D:\\");
    	}
    }

    But how can open it with a double click on an item from my list?
    My website : MediDev

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Open location from JList

    Now you need to go to the Java tutorial and read about how to use JList.
    Go to this site: The Really Big Index
    and Find Lists. There is a section on how to use lists.

  12. The Following User Says Thank You to Norm For This Useful Post:

    hexwind (August 21st, 2011)

  13. #12
    Member hexwind's Avatar
    Join Date
    May 2011
    Location
    Tunisia
    Posts
    48
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Open location from JList

    Quote Originally Posted by Norm View Post
    Now you need to go to the Java tutorial and read about how to use JList.
    Go to this site: The Really Big Index
    and Find Lists. There is a section on how to use lists.
    It worked perfectly !
    I just wasted my time messing around with "\\" in the path, I thought it wouldn't be accepted while it was. anyways. Thanks a bunch for your help sir
    My website : MediDev

  14. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Open location from JList

    Glad to help. Good luck on your future projects.

  15. #14
    Member hexwind's Avatar
    Join Date
    May 2011
    Location
    Tunisia
    Posts
    48
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Open location from JList

    Cheers !
    My website : MediDev

Similar Threads

  1. How to find location of ipaddress using Java
    By tej in forum Java Networking
    Replies: 8
    Last Post: September 8th, 2012, 06:05 AM
  2. output to a specific location in html code
    By aketr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 15th, 2011, 11:44 AM
  3. [SOLVED] How to specify FileOutputStream location?
    By Hallowed in forum Java Theory & Questions
    Replies: 7
    Last Post: May 29th, 2011, 06:36 PM
  4. Image location on a Runnable JAR file?
    By DarrenReeder in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2010, 07:59 AM
  5. [SOLVED] File Reading from the specified location
    By rajesh.mv in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: August 13th, 2009, 12:56 AM