Solved

selecting files from a listbox

Posted on 2013-06-27
6
255 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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb entity bean example 2 57
ejb entity bean example issue 2 43
Is there a simpler dropbox system? 10 53
Java array 10 87
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

739 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