Results 1 to 5 of 5
  1. #1
    Wraithier is offline Member
    Join Date
    Jun 2008
    Posts
    10
    Rep Power
    0

    Default Need JFileChooser Help

    Hello, I am new to these forums and am newer to Java programming (I've only done around 5 or 6 simple programs). This time I think I bit off more than I can chew. I am attempting to create what I thought was a simple program using a GUI to transfer files. The user selects a file through the first JFileChooser, and selects the Destination through the second JFileChooser. Once the copy button is clicked I would like a copy of the source file in the destination directory. Simple huh? The problem I am having is I can't seem to get the files to copy correctly. It doesn't seem that the information from the JFileChooser (getSelectedFile) is passing the file to my copy method. Here is the code I have so far. Any pointers or critiques would be greatly appreciated.

    Thanks in advance!

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.io.*;
    import java.io.File.*;
    
    public class FileCopy extends javax.swing.JFrame implements ActionListener {
    
        JFileChooser jfc;
        JFrame frame;
        public javax.swing.JButton CopyBtn;
        public javax.swing.JButton DestinationBtn;
        public javax.swing.JTextField DestinationTxt;
        public javax.swing.JButton RenameBtn;
        public javax.swing.JButton ResetBtn;
        public javax.swing.JButton SourceBtn;
        public javax.swing.JTextField SourceTxt;
        public javax.swing.JLabel jLabel1;
        public javax.swing.JLabel jLabel2;
        public File input = null;
        public File output = null;
        
        
    
        public FileCopy() {
    
            SourceBtn = new javax.swing.JButton();
            DestinationBtn = new javax.swing.JButton();
            jLabel1 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
            CopyBtn = new javax.swing.JButton();
            DestinationTxt = new javax.swing.JTextField();
            ResetBtn = new javax.swing.JButton();
            RenameBtn = new javax.swing.JButton();
            SourceTxt = new javax.swing.JTextField();
    
            CopyBtn.addActionListener(this);
            ResetBtn.addActionListener(this);
            SourceBtn.addActionListener(this);
            RenameBtn.addActionListener(this);
            DestinationBtn.addActionListener(this);
    
            this.pack();
            this.setSize(250, 60);
            this.setLocationRelativeTo(null);
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Easy Remote File Copy Tool");
            setResizable(false);
    
            SourceBtn.setText("Browse");
    
            DestinationBtn.setText("Browse");
    
            jLabel1.setText("Select  File to Copy");
    
            jLabel2.setText("Select Destination");
    
            CopyBtn.setText("Copy");
    
            CopyBtn.setToolTipText("Copy Selected File to Destination Directory");
    
            ResetBtn.setText("Reset");
            ResetBtn.setToolTipText("Reset");
            ResetBtn.setActionCommand("jButton1");
    
            RenameBtn.setText("Rename");
            RenameBtn.setToolTipText("Rename Selected File and Copy to the Destination Directory");
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGap(96, 96, 96).addComponent(jLabel1).addContainerGap(179, Short.MAX_VALUE)).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap().addComponent(SourceTxt, javax.swing.GroupLayout.DEFAULT_SIZE, 230, Short.MAX_VALUE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(SourceBtn).addGap(48, 48, 48)).addGroup(layout.createSequentialGroup().addGap(100, 100, 100).addComponent(jLabel2).addContainerGap(181, Short.MAX_VALUE)).addGroup(layout.createSequentialGroup().addContainerGap().addComponent(DestinationTxt, javax.swing.GroupLayout.DEFAULT_SIZE, 230, Short.MAX_VALUE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(DestinationBtn).addGap(52, 52, 52)).addGroup(layout.createSequentialGroup().addContainerGap().addComponent(RenameBtn).addGap(18, 18, 18).addComponent(CopyBtn).addGap(18, 18, 18).addComponent(ResetBtn).addContainerGap(126, Short.MAX_VALUE)));
            layout.setVerticalGroup(
                    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGap(21, 21, 21).addComponent(jLabel1).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(SourceBtn).addComponent(SourceTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)).addGap(18, 18, 18).addComponent(jLabel2).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(DestinationTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(DestinationBtn)).addGap(18, 18, 18).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(RenameBtn).addComponent(CopyBtn).addComponent(ResetBtn)).addGap(30, 30, 30)));
    
            pack();
           
        }       
        
      public class ChooseInputFile{    
       
         
      public void ChooseInputFile(){
      
          
      JFileChooser jfc = new JFileChooser();    
      jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
      int returnVal = jfc.showOpenDialog(FileCopy.this);
      if(returnVal == JFileChooser.APPROVE_OPTION){
         input = jfc.getSelectedFile();
         SourceTxt.setText(jfc.getSelectedFile().getName() );
      }    
       
      }  
      }
          
      public class ChooseOutputDirectory{
          
      public void ChooseOutputDirectory(){
          
      JFileChooser jfc = new JFileChooser();
      jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
      int option = jfc.showOpenDialog(FileCopy.this);
      if(option == JFileChooser.APPROVE_OPTION){
          output = jfc.getSelectedFile();
          DestinationTxt.setText(jfc.getSelectedFile().getName() );
      }
      }
      }
      
        public class CopyFile { 
            
            
    
            public void CopyFile(File input, File output) {     
                
                try {
                    FileInputStream fis = new FileInputStream(input);
                    FileOutputStream fos = new FileOutputStream(output);
    
                    int read;
                    byte[] buffer = new byte[65536];
                    while ((read = fis.read(buffer)) != -1) {
                        fos.write(buffer, 0, read);
                    }
                } catch (Exception e) {
                    System.err.println("Exception occured:");
                    e.printStackTrace();
                }
             CopyFile(input, new File(output + File.separator + input.getName()));
              System.out.println("Copying done");
            System.exit(0);
            }
        
          
        }
    
        public void actionPerformed(ActionEvent e) {
             
            if (e.getSource() == SourceBtn) {
                ChooseInputFile cif = new ChooseInputFile();      
                cif.ChooseInputFile();    
                
                }
    
            
            if (e.getSource() == DestinationBtn) {
                ChooseOutputDirectory cof = new ChooseOutputDirectory();
                cof.ChooseOutputDirectory();            
                }
            
            if (e.getSource() == CopyBtn) {  
                CopyFile cf = new CopyFile();
                cf.CopyFile(input, output);
            
            }
        }
    
        
    
        public static void main(String args[]) throws IOException {
            FileCopy fc = new FileCopy();
            fc.setVisible(true);
            
        }
    
           
        }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.event.*;
    import java.io.*;
    import javax.swing.*;
    
    public class FileCopyRx extends javax.swing.JFrame implements ActionListener {
        JFileChooser jfc = new JFileChooser(".");
        JFrame frame;
        public javax.swing.JButton CopyBtn;
        public javax.swing.JButton DestinationBtn;
        public javax.swing.JTextField DestinationTxt;
        public javax.swing.JButton RenameBtn;
        public javax.swing.JButton ResetBtn;
        public javax.swing.JButton SourceBtn;
        public javax.swing.JTextField SourceTxt;
        public javax.swing.JLabel jLabel1;
        public javax.swing.JLabel jLabel2;
        public File input = null;
        public File output = null;
        
        
    
        public FileCopyRx() {
            SourceBtn = new javax.swing.JButton();
            DestinationBtn = new javax.swing.JButton();
            jLabel1 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
            CopyBtn = new javax.swing.JButton();
            DestinationTxt = new javax.swing.JTextField();
            ResetBtn = new javax.swing.JButton();
            RenameBtn = new javax.swing.JButton();
            SourceTxt = new javax.swing.JTextField();
    
            CopyBtn.addActionListener(this);
            ResetBtn.addActionListener(this);
            SourceBtn.addActionListener(this);
            RenameBtn.addActionListener(this);
            DestinationBtn.addActionListener(this);
    
    //        this.pack();
            this.setSize(250, 60);
            this.setLocationRelativeTo(null);
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Easy Remote File Copy Tool");
            setResizable(false);
    
            SourceBtn.setText("Browse");
    
            DestinationBtn.setText("Browse");
    
            jLabel1.setText("Select  File to Copy");
    
            jLabel2.setText("Select Destination");
    
            CopyBtn.setText("Copy");
    
            CopyBtn.setToolTipText("Copy Selected File to Destination Directory");
    
            ResetBtn.setText("Reset");
            ResetBtn.setToolTipText("Reset");
            ResetBtn.setActionCommand("jButton1");
    
            RenameBtn.setText("Rename");
            RenameBtn.setToolTipText("Rename Selected File and Copy to the 
    
    Destination Directory");
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout
    
    (getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                    layout.createParallelGroup
    
    (javax.swing.GroupLayout.Alignment.LEADING).addGroup
    
    (layout.createSequentialGroup().addGap(96, 96, 96).addComponent
    
    (jLabel1).addContainerGap(179, Short.MAX_VALUE)).addGroup
    
    (javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup
    
    ().addContainerGap().addComponent(SourceTxt, 
    
    javax.swing.GroupLayout.DEFAULT_SIZE, 230, Short.MAX_VALUE).addPreferredGap
    
    (javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent
    
    (SourceBtn).addGap(48, 48, 48)).addGroup(layout.createSequentialGroup().addGap
    
    (100, 100, 100).addComponent(jLabel2).addContainerGap(181, 
    
    Short.MAX_VALUE)).addGroup(layout.createSequentialGroup().addContainerGap
    
    ().addComponent(DestinationTxt, javax.swing.GroupLayout.DEFAULT_SIZE, 230, 
    
    Short.MAX_VALUE).addPreferredGap
    
    (javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent
    
    (DestinationBtn).addGap(52, 52, 52)).addGroup(layout.createSequentialGroup
    
    ().addContainerGap().addComponent(RenameBtn).addGap(18, 18, 18).addComponent
    
    (CopyBtn).addGap(18, 18, 18).addComponent(ResetBtn).addContainerGap(126, 
    
    Short.MAX_VALUE)));
            layout.setVerticalGroup(
                    layout.createParallelGroup
    
    (javax.swing.GroupLayout.Alignment.LEADING).addGroup
    
    (layout.createSequentialGroup().addGap(21, 21, 21).addComponent
    
    (jLabel1).addPreferredGap
    
    (javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup
    
    (layout.createParallelGroup
    
    (javax.swing.GroupLayout.Alignment.BASELINE).addComponent
    
    (SourceBtn).addComponent(SourceTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 
    
    javax.swing.GroupLayout.DEFAULT_SIZE, 
    
    javax.swing.GroupLayout.PREFERRED_SIZE)).addGap(18, 18, 18).addComponent
    
    (jLabel2).addPreferredGap
    
    (javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addGroup
    
    (layout.createParallelGroup
    
    (javax.swing.GroupLayout.Alignment.BASELINE).addComponent(DestinationTxt, 
    
    javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, 
    
    javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(DestinationBtn)).addGap(18, 
    
    18, 18).addGroup(layout.createParallelGroup
    
    (javax.swing.GroupLayout.Alignment.BASELINE).addComponent
    
    (RenameBtn).addComponent(CopyBtn).addComponent(ResetBtn)).addGap(30, 30, 30)));
    
            pack();
           
        }       
        
        private void setInputFile() {
    //        JFileChooser jfc = new JFileChooser();
            jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
            int returnVal = jfc.showOpenDialog(FileCopyRx.this);
            if(returnVal == JFileChooser.APPROVE_OPTION){
                input = jfc.getSelectedFile();
                SourceTxt.setText(jfc.getSelectedFile().getName() );
            }
        }
          
        private void setOutputDirectory() {
    //        JFileChooser jfc = new JFileChooser();
            jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int option = jfc.showOpenDialog(FileCopyRx.this);
            if(option == JFileChooser.APPROVE_OPTION){
                output = jfc.getSelectedFile();
                DestinationTxt.setText(jfc.getSelectedFile().getName() );
            }
        }
    
        private void copyFile() { 
            if(input == null || output == null)
                return;
            try {
                String name = input.getName();
                File file = new File(output, name);
                System.out.println("file = " + file.getPath());
                if(!file.exists())
                    file.createNewFile();
    
                BufferedReader br = new BufferedReader(
                                    new InputStreamReader(
                                    new FileInputStream(input)));
                BufferedWriter bw = new BufferedWriter(
                                    new OutputStreamWriter(
                                    new FileOutputStream(file)));
                String line;
                while((line = br.readLine()) != null) {
                    bw.write(line, 0, line.length());
                    bw.newLine();
                }
                br.close();
                bw.close();
    /*
                FileInputStream fis = new FileInputStream(input);
                FileOutputStream fos = new FileOutputStream(output);
    
                int read;
                byte[] buffer = new byte[65536];
                while ((read = fis.read(buffer)) != -1) {
                    fos.write(buffer, 0, read);
                }
    */
            } catch (IOException e) {
                System.err.println("Exception occured:");
                System.out.println(e.getMessage());
            }
            System.out.println("Copying done");
            System.exit(0);
        }
    
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == SourceBtn) {     
                setInputFile();
            }
    
            if (e.getSource() == DestinationBtn) {
                setOutputDirectory();
            }
            
            if (e.getSource() == CopyBtn) {
                copyFile();
            }
        }
    
        
    
        public static void main(String args[]) throws IOException {
            FileCopyRx fc = new FileCopyRx();
            fc.setVisible(true);
        }
    }
    Last edited by hardwired; 06-17-2008 at 11:58 PM. Reason: Forgot/added the newLine() call.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Lots of discussions are going on here in our community regarding the JFileChooser. Search the forum may helps you too.

  4. #4
    Wraithier is offline Member
    Join Date
    Jun 2008
    Posts
    10
    Rep Power
    0

    Default

    Thanks Hardwired, that really helped a lot. Like I said I couldn't figure out why the info wasn't passing from the JFileChooser into my variables. One more question, if I want to be able to copy entire folders as well as files how difficult would that be? I don't want to make this simple program more complicated than it should be. Thanks again!

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    if I want to be able to copy entire folders as well as files how difficult would that be
    Not too bad. Just do a little at a time. The File api has what you will need to do this. Try copying all files in the input folder first. When you get that down then work on the folder part. For this you can make up a recursive method that will traverse the folder hierarchy, ie, child folders within parents. Then you can use what you've developed before to copy the files from/in each folder as you go.

Similar Threads

  1. JFileChooser example (selecting a directory)
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 02-03-2009, 01:25 PM
  2. Localize JFileChooser
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-14-2008, 11:54 AM
  3. JFileChooser remember the location
    By Mr tuition in forum AWT / Swing
    Replies: 3
    Last Post: 12-08-2007, 05:17 PM
  4. JFileChooser/Scanner/openFileReader
    By Feng in forum New To Java
    Replies: 5
    Last Post: 11-24-2007, 04:31 PM
  5. how to use JFileChooser
    By tommy in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 08:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •