Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 258
  • Last Modified:

selecting files from a listbox

Hi:
I have a little java utility which copies  certain files from x to y. A JList displays the contents of the files in y. From y I need the user to select the files he needs and then copy them to z. At the moment I can copy all the files  in y but this is a bit of a blunt instrument . Is there a way that the user can select from a list, then copy just those files identified in that list?
Thanks!
0
ClaytonGlass
Asked:
ClaytonGlass
  • 3
  • 2
1 Solution
 
TvMptCommented:
I dont really understand your question... :)

Can you use the JFileChooser? Its allow multiple select files.

http://docs.oracle.com/javase/6/docs/api/javax/swing/JFileChooser.html
0
 
ClaytonGlassAuthor Commented:
Thanks for the prompt reply. THe utility runs on a glass cutting table.
directory x on the network stores all files for all tables in the organisation. My utility allows the user to select those files that relate to his table and copy them to directory y on the pc on his table. THese display in JList so the user can see his workload for the day. I would like the user to select those files he want to transfer directly to the cutting table (directory z); at present it dumps them all. I had never heard of a filechooser but am willing to look at it if you think it works.
0
 
krakatoaCommented:
You can work with the List approach that you already have.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
krakatoaCommented:
If you use nio, you can achieve rapid moves by something like this :

import java.awt.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;


class FileMover extends JFrame implements ActionListener {

static File file;
static File dir;
static File[] files;
static java.awt.List list;
static JButton jb;
static String[] sa;


public static void main(String[] args){

	FileMover fm = new FileMover();
	
	fm.setLayout(new BorderLayout());
	fm.setSize(new Dimension(200,200));
	fm.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	jb = new JButton("Move files");
	jb.addActionListener(fm);
	list = new java.awt.List();
	list.setMultipleMode(true);
	dir = new File("C:/ee_q_code/f1"); //change this to an appropriate directory

	sa = dir.list();

	

	for(int a = 0;a<sa.length;a++){list.add(sa[a]);}

	fm.getContentPane().add(list,BorderLayout.NORTH);
	fm.getContentPane().add(jb,BorderLayout.SOUTH);
	fm.setVisible(true);

}



public void actionPerformed(ActionEvent ev){

try{

sa = list.getSelectedItems();

for(int a = 0;a<sa.length;a++){

	File f = new File("C:/EE_Q_CODE/f1/"+sa[a]); //change this to an appropriate directory
	
	File fnew = new File("C:/EE_Q_CODE/f2/"); //change this to an appropriate directory
	
	Path sourceDir = f.toPath();
	Path targetDir = fnew.toPath();

	Files.move(sourceDir, targetDir.resolve(f.getName()),  StandardCopyOption.REPLACE_EXISTING);
  
  }

  }catch(Exception exc){exc.printStackTrace();}

 }

}

Open in new window

0
 
ClaytonGlassAuthor Commented:
Thank you very much!
0
 
krakatoaCommented:
ok.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now