Solved

selecting files from a listbox

Posted on 2013-06-27
6
251 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
  • 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

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 contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now