Solved

selecting files from a listbox

Posted on 2013-06-27
6
256 Views
Last Modified: 2013-06-28
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
Comment
Question by:ClaytonGlass
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 9

Expert Comment

by:TvMpt
ID: 39280580
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
 

Author Comment

by:ClaytonGlass
ID: 39280590
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
 
LVL 16

Expert Comment

by:krakatoa
ID: 39284179
You can work with the List approach that you already have.
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 16

Accepted Solution

by:
krakatoa earned 250 total points
ID: 39284369
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
 

Author Closing Comment

by:ClaytonGlass
ID: 39284435
Thank you very much!
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 39284444
ok.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question