Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java JFileChooser GUI

Options
  • 14-03-2006 3:17am
    #1
    Registered Users Posts: 3,579 ✭✭✭


    Ok, I have my main code working and want to stick a nice GUI onto it, which I also have working, both files are seperate at the moment and I want to mush them together. :p

    The base GUI code I'm using is here:
    http://www.java2s.com/Code/Java/Swing-JFC/Asimplefilechoosertoseewhatittakestomakeoneofthesework.htm
    Mine is slightly different in that I changed some code around to include different buttons, statusbars and FileFilters.

    And here's what I have in the main program as a placeholder, it takes an input text file from the console terminal and assigns the input to a file Reader, Scanner and PrintWriter input-output thing:
    ...
    Scanner console = new Scanner(System.in);
    System.out.print("Enter the path and filename of the input file: ");
    String inputFileName = console.next();
    System.out.print("Enter the path and filename of the output file: ");
    String outputFileName = console.next();
    
    try {
    
     // File input output streams
     FileReader reader = new FileReader(inputFileName);
     Scanner in = new Scanner(reader);
     PrintWriter out  = new PrintWriter(outputFileName);
    ...
    
    So, it takes an input text file, performs some process on the data in that file and outputs the processed data to another specified file.

    GUI: http://img8.picsplace.to/img.php?file=img8/10/Clipboard01.jpg
    What I want is the user to be able to pick the input file (Open button) and output file (Save button) from the GUI then press Run button and the process of the main program on the file starts.

    I think I need to put in something like this after each FileChooser is created:
    String inputFileName = setSelectedFiles();
    //and
    String outputFileName = setSelectedFiles();
    

    I know I need to put something like those lines in the blocks of code here somewhere, most likely near the if statements:
    // Create a file chooser that opens up as an Open dialog
    openButton.addActionListener(new ActionListener() {
    	public void actionPerformed(ActionEvent ae) {
    	String[] gml = new String[] { "gml", "GML" };
    	JFileChooser chooser = new JFileChooser();
    
    	chooser.setMultiSelectionEnabled(false);
    	chooser.addChoosableFileFilter(new SimpleFileFilter(gml,"GML Files (*.gml, *.GML)"));
    
    	int option = chooser.showOpenDialog(FileChooser.this);
    	if (option == JFileChooser.APPROVE_OPTION) {
    		File[] sf = chooser.getSelectedFiles();
    		String filelist = "nothing";
    		if (sf.length > 0) filelist = sf[0].getName();
    			for (int i = 1; i < sf.length; i++) {
    				filelist += ", " + sf[i].getName();
    			}
    		statusbar1.setText("GML Input: " + filelist + "  ");
    		statusbar3.setText("");
    	}
    	else {
    		statusbar3.setText("Canceled opening GML...");
    	}
    }
    });
    
    // Create a file chooser that opens up as a Save dialog
    saveButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
    	String[] vrml = new String[] { "wrl", "WRL" };
    	JFileChooser chooser = new JFileChooser();
    
    	chooser.setMultiSelectionEnabled(true);
    	chooser.addChoosableFileFilter(new SimpleFileFilter(vrml,"VRML Files (*.wrl, *.WRL)"));
    
    	int option = chooser.showSaveDialog(FileChooser.this);
    	if (option == JFileChooser.APPROVE_OPTION) {
    		statusbar2.setText("VRML Output: " + ((chooser.getSelectedFile()!=null)?
    		chooser.getSelectedFile().getName():"nothing"));
    		statusbar4.setText("");
    	}
    	else {
    		statusbar4.setText("Canceled saving VRML...");
    	}
    }
    });
    
    // Run Program
    runButton.addActionListener(new ActionListener() {
    	public void actionPerformed(ActionEvent ae) {
    
    // MAIN PROGRAM PROCESS GOES HERE
    // processes the input file, outputs to the output file
    
    	statusbar3.setText("Conversion complete!");
    	statusbar4.setText("Open your file from the location you saved.");
    	}
    });
    

    Any help or clarification would be appreciated.


Comments

  • Registered Users Posts: 3,579 ✭✭✭BopNiblets


    Whoa whoa whoa, don't all reply at once!

    Ok, I did half on my own (the second half), I'm nearly sure this will do the output file bit...
    File outputFileName = chooser.getSelectedFile();
    //and
    try {
      PrintWriter out  = new PrintWriter(outputFileName);
    }
    catch (IOException exception) {
      System.out.println("Error processing file: " + exception);
    }
    


Advertisement