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

Thread: GOing from VB to Java

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default GOing from VB to Java

    I have an app that was written in VB and I would like to create the same app in JAVA, so that it works on all platforms.
    I have a button that when clicked opens the specified file in a text box.

    OpenDocument(txtInput.Text);

    In my JAVA app, I am trying Document.open(txtinput.getText())

    That does not work. NetBeans in stelling me "Can't find Synmbol, Symbol: variable Document.

    So how can I use the defualt text editor on MAC, LINUX, or Windows to open the file specified?


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: GOing from VB to Java

    Wait, I'm confused. Are you trying to open the file in a text box (ie: part of the GUI) or are you trying to get the file to open up in its default editor (ie: opening the document in MS Word or something)?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: GOing from VB to Java

    Yes I want the file to open in wordpad, or textedit(MAC) or the equivalent on Linux.

  4. #4
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: GOing from VB to Java

    The JVM can not run other programs directly, its a virtual machine and this is part of its build in malware security.
    There is however the exec() method in the Runtime class which can be used to deliver command line commands to the OS.
    You can potentially use that to have the OS open a text document but there is no guarantee that it will actually work on all operating systems.

    There is also the ProcessBuilder class which might also work to get things done, but I never used it and cant quite tell you anything about it.

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: GOing from VB to Java

    Try:
    java.awt.Desktop.getDesktop().edit(file);
    I'm told there might be issues with earlier versions of Windows (XP and 2003) while using this. file would be a File (File (Java Platform SE 7 )) object, which you can create from the path of the file the user requested.


    @Cornix: While you are mostly correct, Java does support some ways of accessing system resources. Most commonly, these resources can be accessed through classes like System and Desktop (I don't know of any class specifically dedicated to accessing system resources, these can just be used as work-arounds). Consider the fact that if the JVM couldn't access any system resources, printing would be impossible (but you can print from Java).
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. #6
    Junior Member
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: GOing from VB to Java

    So here is what I put.

    java.awt.Desktop.getDesktop().edit(txtInput.getTex t());

    NetBeans says, "Incompatible types: String cannot be converted to file. txtInput.getText()

  7. #7
    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: GOing from VB to Java

    String cannot be converted to file.
    Time to read the API doc.
    getText() returns a String
    edit() takes a File object

    A String object can not be converted (by the compiler) to a File object. The programmer will have to write the code to do that.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: GOing from VB to Java

    To reiterate what Norm said, you need to create a File object (look at my previous post for a link to the API document for the File class).
    What does txtInput.getText() represent? Is it a file path or something?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  9. #9
    Junior Member
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: GOing from VB to Java

    So the user selects a file and the file and path get put into the the text box called "txtInput". I want the user to be able to click a button that will open that file in a text editor. I will look at the APC.

  10. #10
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: GOing from VB to Java

    Then you need to say:
    java.awt.Desktop.getDesktop().edit(new File(txtInput.getText()));
    (there are obviously other things you should do, like checking input, but this is how you create a new file and use the method)
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/