Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-17-2008, 10:24 PM
Member
 
Join Date: Jun 2008
Posts: 10
Wraithier is on a distinguished road
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!

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); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-18-2008, 01:54 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
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-18-2008 at 01:58 AM. Reason: Forgot/added the newLine() call.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-18-2008, 06:17 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Lots of discussions are going on here in our community regarding the JFileChooser. Search the forum may helps you too.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-18-2008, 03:22 PM
Member
 
Join Date: Jun 2008
Posts: 10
Wraithier is on a distinguished road
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!
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-18-2008, 07:40 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
JFileChooser example (selecting a directory) Java Tip Java Tips 0 03-14-2008 02:02 PM
Localize JFileChooser Java Tip Java Tips 0 03-14-2008 01:54 PM
JFileChooser remember the location Mr tuition AWT / Swing 3 12-08-2007 07:17 PM
JFileChooser/Scanner/openFileReader Feng New To Java 5 11-24-2007 06:31 PM
how to use JFileChooser tommy New To Java 1 08-06-2007 10:49 PM


All times are GMT +3. The time now is 01:24 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org