simple JAVA FileBrowser

Stuck with a problem in your code? Seek help here.

simple JAVA FileBrowser

Postby pongWiz » December 13th, 2012, 10:01 pm

I am having a bit of a problem. The code below will browse to look for a file but it wont actually open/ run it. I could choose JFileChooser but for pure aesthetics i like fileDialog better.

Code: Select all
import java.awt.*;

public class UseFileDialog {
   public String loadFile (Frame f, String title, String defDir, String fileType) {
            
         FileDialog fd = new FileDialog(f, title, FileDialog.LOAD);
         fd.setFile(fileType);
         fd.setDirectory(defDir);
         fd.setLocation(50, 50);
         fd.setVisible(true);
         
         return fd.getDirectory()+fd.getFile();
      }
      
   /*public String saveFile(Frame f, String title, String defDir, String fileType) {
            
         FileDialog fd = new FileDialog(f, title, FileDialog.SAVE);
         fd.setFile(fileType);
         fd.setDirectory(defDir);
         fd.setLocation(50, 50);
         fd.setVisible(true);
            
         return fd.getFile();
      }*/
   public static void main(String s[]) {
      
      UseFileDialog ufd = new UseFileDialog();
      
      System.out.println("Loading : "+ ufd.loadFile(new Frame(), "Open...", ".\\", "."));//.java allows only .java files
      //System.out.println("Saving : "+ ufd.saveFile(new Frame(), "Save...", ".\\", "."));
      System.exit(0);
      }
}
pongWiz
 
Posts: 69
Joined: September 9th, 2011, 9:13 pm

Re: simple JAVA FileBrowser

Postby GaspingSpark » December 15th, 2012, 8:48 pm

File pickers like FileDialog only return the name of the file that the user picked. *You* have to write the code that figures out what to do with the selected file. :)

Here's code that uses Desktop.getDesktop().open() to ask the operating system to launch the default program with the selected file. Note the added imports at the top and the required exception handling try/catch.

Code: Select all
import java.awt.*;
import java.io.File;
import java.io.IOException;

public class UseFileDialog {
   public String loadFile (Frame f, String title, String defDir, String fileType) {
           
         FileDialog fd = new FileDialog(f, title, FileDialog.LOAD);
         fd.setFile(fileType);
         fd.setDirectory(defDir);
         fd.setLocation(50, 50);
         fd.setVisible(true);
         
         return fd.getDirectory()+fd.getFile();
      }
     
   /*public String saveFile(Frame f, String title, String defDir, String fileType) {
           
         FileDialog fd = new FileDialog(f, title, FileDialog.SAVE);
         fd.setFile(fileType);
         fd.setDirectory(defDir);
         fd.setLocation(50, 50);
         fd.setVisible(true);
           
         return fd.getFile();
      }*/
   public static void main(String s[]) {

      UseFileDialog ufd = new UseFileDialog();

      //File dialogs only return the filename that the user picked
      String filename = ufd.loadFile(new Frame(), "Open...", ".\\", ".");

      //Convert it to a File - need to import java.io
      File picked_file = new File(filename);

      //Print out the message
      System.out.println("Loading : "+ filename);//.java allows only .java files

      //Ask the operating system to open the specified file using
      //whatever the default assigned program is.
      try {
         Desktop.getDesktop().open(picked_file);
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }

      //System.out.println("Saving : "+ ufd.saveFile(new Frame(), "Save...", ".\\", "."));
      System.exit(0);
   }
}

GaspingSpark
 
Posts: 180
Joined: March 8th, 2011, 10:24 am


Return to Help Me! Software

Who is online

Users browsing this forum: No registered users and 2 guests