Solved

selecting files from a listbox

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

 
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
fibonacci ten numbers 4 54
jsp login check 12 52
going to wrong jsp page 2 47
web project error add remove 1 51
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

756 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