java - Getting multiple files from JFileChooser -
java - Getting multiple files from JFileChooser -
in gui app working on, require select multiple files instead of straight opening file chooser first need add together required files in selected list (so instead of selecting files 1 time again , 1 time again different directories can select them @ time , open files added list). should able remove multiple files nowadays in selected file list too.
is possible jfilechooser or need design 1 per requirements?
what looking not standard feature, can customize chooser, using jfilechooser.setaccessory(...)
takes argument jcomponent
. can create panel list can add together , remove selected files (or other jcomponent
want create) , add together accessory file chooser.
see filechooserdemo2 more explanation on this.
here's example. created jlist can add together selecting files, , remove files selecting file list , clicking remove. when click open, files can obtained defaultlistmodel
filelistaccessory
class extends jcomponent
import java.awt.borderlayout; import java.awt.dimension; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.beans.propertychangeevent; import java.beans.propertychangelistener; import java.io.file; import javax.swing.defaultlistmodel; import javax.swing.jbutton; import javax.swing.jcomponent; import javax.swing.jfilechooser; import javax.swing.jlist; import javax.swing.jscrollpane; import javax.swing.border.emptyborder; public class filelistaccessory extends jcomponent implements propertychangelistener { private file file = null; private defaultlistmodel model; private jlist list; private jbutton removeitem; public filelistaccessory(jfilechooser chooser) { chooser.addpropertychangelistener(this); model = new defaultlistmodel(); list = new jlist(model); jscrollpane pane = new jscrollpane(list); pane.setpreferredsize(new dimension(200, 250)); removeitem = createremoveitembutton(); setborder(new emptyborder(10, 10, 10, 10)); setlayout(new borderlayout()); add(pane); add(removeitem, borderlayout.south); } public defaultlistmodel getmodel() { homecoming model; } private void addfiletolist() { model.addelement(file); } private void removefilefromlist() { if (list.getselectedindex() != -1) { model.remove(list.getselectedindex()); } } private jbutton createremoveitembutton() { jbutton button = new jbutton("remove"); button.addactionlistener(new actionlistener(){ @override public void actionperformed(actionevent e) { removefilefromlist(); } }); homecoming button; } @override public void propertychange(propertychangeevent e) { boolean update = false; string prop = e.getpropertyname(); //if directory changed, don't if (jfilechooser.directory_changed_property.equals(prop)) { file = null; update = true; //if file became selected, find out one. } else if (jfilechooser.selected_file_changed_property.equals(prop)) { file = (file) e.getnewvalue(); update = true; } if (update && file != null) { addfiletolist(); } } }
launcher
import java.io.file; import javax.swing.defaultlistmodel; import javax.swing.jfilechooser; import javax.swing.swingutilities; public class javaapplication4 { public static void main(string[] args) { swingutilities.invokelater(new runnable() { @override public void run() { jfilechooser fc = new jfilechooser(); filelistaccessory accessory = new filelistaccessory(fc); fc.setaccessory(accessory); int open = fc.showopendialog(fc); if (open == jfilechooser.approve_option) { defaultlistmodel model = accessory.getmodel(); (int = 0; < model.getsize(); i++) { system.out.println(((file)model.getelementat(i)).getname()); } } } }); } }
java swing user-interface jlist jfilechooser
Comments
Post a Comment