Hi,
I’m trying to code a small program which allows us to select multiple files from a folder and save the content of all files previously selected in one file.
The code is very well without GUI (in console).
But now I want to do the same with a GUI.
I coded my GUI with NetBeans 6.9 and the content is:

-Browse button : we can select multiple files from a folder.
-a TextArea: which displays the previous selected files.

-Save As button: this button must open a Save As window and we could choose the name of the file that we want to save (this file have the content of all files previously selected in one file).
-another TextArea: which must display only the file name.

The Browse button and its TextArea are working very well.
But when I try to add method if actionPerformed for the the second button (Save As)and its TextArea I have some trouble.

Her e is the code snipet:

For Browse Button and TextArea is:

…..
private void browsefilebuttonActionPerformed(java.awt.event.ActionEvent evt) {                                                 
        final JFileChooser fc=new JFileChooser();
        fc.setMultiSelectionEnabled(true);
        int returnVal = fc.showOpenDialog(SOHJoinerUI);
        if(returnVal == JFileChooser.APPROVE_OPTION)

    {
        File[] files = fc.getSelectedFiles();
        for (int i=0; i<files.length;i++)
        {
            jTextAreaSource.append(files[i].getName());
            jTextAreaSource.append("\n");
        }
    }                                                
    }
……

For Save As Button and its TextArea is:

……
private void saveasfilebuttonActionPerformed(java.awt.event.ActionEvent evt) {                                                 
        final JFileChooser fc=new JFileChooser();
        fc.setMultiSelectionEnabled(true);
        int returnVal = fc.showSaveDialog(SOHJoinerUI);
        if(returnVal == JFileChooser.APPROVE_OPTION)

    {
        fc.getSelectedFiles();
       
            jTextAreaDirectory.append(files[i].getName());
            jTextAreaDirectory.append("\n");
        }
    }                                                
    }
……..

Could someone clarify my mistakes.

For information the following code is working well when it’s launched alone.

import java.io.File;

import javax.swing.JFileChooser;

public class JFChooser

{
  public static void main(String[] argv) throws Exception{
    JFileChooser chooser = new JFileChooser();
    //File f = new File(new File("filename.txt").getCanonicalPath());
    chooser.setSelectedFile(new File("c:/Temp2/Filename.txt") );
    chooser.showSaveDialog(null);
    File curFile = chooser.getSelectedFile();
  }
}

i hope to be clear.
Thanks

The following code from James Cherill is working but i can't display the name of the file in the textarea.

private void saveasdirectorybuttonActionPerformed(java.awt.event.ActionEvent evt) {

        //JFileChooser fc = new JFileChooser();
        //fc.showSaveDialog(null);
        //File outFile = fc.getSelectedFile();
        //System.out.println(outFile.getName());
              JFileChooser fc = new JFileChooser();
    //File f = new File(new File("filename.txt").getCanonicalPath());
              fc.setSelectedFile(new File("c:/Temp/anyFilename.txt") );
              fc.showSaveDialog(SOHJoinerUI);
              File curFile = fc.getSelectedFile();
              JTextAreaOutputDirectory.append(curFile.getName());
        }

How could put the name of the filename in the textarea?

thanks

The solution is:
JTextAreaOutputDirectory.append(curFile.getName()); instead of jTextAreaOutputDirectory.append(curFile.getName());

and to add JTextAreaOutputDirectory.setText("");

now i can display the filename and clear the jTextArea with my clear button

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.