List of usage examples for javax.swing JFileChooser setFileSelectionMode
@BeanProperty(preferred = true, enumerationValues = { "JFileChooser.FILES_ONLY", "JFileChooser.DIRECTORIES_ONLY", "JFileChooser.FILES_AND_DIRECTORIES" }, description = "Sets the types of files that the JFileChooser can choose.") public void setFileSelectionMode(int mode)
JFileChooser
to allow the user to just select files, just select directories, or select both files and directories. From source file:com.strath.view.MainGUI.java
private void projectSelectMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_projectSelectMouseReleased JFileChooser chooser = new JFileChooser(); chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); int returnValue = chooser.showOpenDialog(null); if (returnValue == JFileChooser.APPROVE_OPTION) { File directory = chooser.getSelectedFile(); String[] extensions = new String[1]; extensions[0] = "java"; selectedProject = listFiles(directory, extensions, true); isProjectSet = true;//from w ww.j a v a 2s. co m } }
From source file:aurelienribon.gdxsetupui.ui.panels.ConfigUpdatePanel.java
private void browse() { String path = Ctx.cfgUpdate.destinationPath; JFrame frame = (JFrame) SwingUtilities.getWindowAncestor(this); JFileChooser chooser = new JFileChooser(new File(path)); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); chooser.setDialogTitle("Select the core project folder"); if (chooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) { pathField.setText(chooser.getSelectedFile().getPath()); updateConfig(chooser.getSelectedFile()); updatePanel();/*from w w w .j ava2s . com*/ } }
From source file:be.ugent.maf.cellmissy.gui.controller.TracksWriterController.java
/** * Choose Directory/*www . ja v a 2 s . c o m*/ */ private void chooseDirectory() { // Open a JFile Chooser JFileChooser fileChooser = new JFileChooser(); fileChooser.setDialogTitle("Select directory to save the files"); fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); fileChooser.setAcceptAllFileFilterUsed(false); // in response to the button click, show open dialog int returnVal = fileChooser.showOpenDialog(tracksWriterDialog); if (returnVal == JFileChooser.APPROVE_OPTION) { directory = fileChooser.getSelectedFile(); } tracksWriterDialog.getDirectoryTextField().setText(directory.getAbsolutePath()); }
From source file:MyFormApp.java
private void AddbuttonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_AddbuttonMouseClicked // TODO add your handling code here: //? ?//from w w w . ja v a 2 s . co m JFileChooser fileChooser = new JFileChooser(); //? fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("PDF Documents", "pdf"));//?pdf fileChooser.setAcceptAllFileFilterUsed(false); int returnValue = fileChooser.showOpenDialog(null); if (returnValue == JFileChooser.APPROVE_OPTION) {//???? File selectedFile = fileChooser.getSelectedFile(); try { pdfToimage(selectedFile); //??? } catch (IOException ex) { Logger.getLogger(MyFormApp.class.getName()).log(Level.SEVERE, null, ex); } System.out.println(selectedFile.getName()); //?? File source = new File("" + selectedFile); File dest = new File(PATH + selectedFile.getName()); //copy file conventional way using Stream long start = System.nanoTime(); //copy files using apache commons io start = System.nanoTime(); int a = i + 1; String imagename = FilenameUtils.removeExtension(selectedFile.getName()); model.addElement(new Book(selectedFile.getName(), "" + a, imagename, PATH)); //list i = i + 1; jList2.setModel(model); jList2.setCellRenderer(new BookRenderer()); try { copyFileUsingApacheCommonsIO(source, dest); //? } catch (IOException ex) { Logger.getLogger(MyFormApp.class.getName()).log(Level.SEVERE, null, ex); } System.out.println("Time taken by Apache Commons IO Copy = " + (System.nanoTime() - start)); } }
From source file:dylemator.UserList.java
private void exportButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exportButtonActionPerformed if (this.filenameCombo.getSelectedIndex() == 0) return;/*from www . j a v a 2s . c o m*/ String sheetName = (String) this.filenameCombo.getSelectedItem(); Workbook wb = new XSSFWorkbook(); Sheet sheet = wb.createSheet(sheetName); Row headerRow = sheet.createRow(0); String[] headers = exportData.get(0); int numOfColumns = headers.length; for (int i = 0; i < numOfColumns; i++) { Cell cell = headerRow.createCell(i); cell.setCellValue(headers[i]); } int rowCount = exportData.size(); for (int rownum = 1; rownum < rowCount; rownum++) { Row row = sheet.createRow(rownum); String[] values = exportData.get(rownum); for (int i = 0; i < numOfColumns; i++) { Cell cell = row.createCell(i); cell.setCellValue(values[i]); } } String defaultFilename = "Export.xlsx"; JFileChooser f = new JFileChooser(System.getProperty("user.dir")); f.setSelectedFile(new File(defaultFilename)); f.setDialogTitle("Wybierz nazw dla pliku eksportu"); f.setFileSelectionMode(JFileChooser.FILES_ONLY); FileFilter ff = new FileFilter() { @Override public boolean accept(File file) { if (file.getName().endsWith(".xlsx")) return true; return false; } @Override public String getDescription() { return ""; } }; f.setFileFilter(ff); File file = null; int save = f.showSaveDialog(this); if (save == JFileChooser.APPROVE_OPTION) file = f.getSelectedFile(); else return; FileOutputStream out; try { out = new FileOutputStream(file); wb.write(out); out.close(); } catch (FileNotFoundException ex) { Logger.getLogger(UserList.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(UserList.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:com.akman.excel.view.frmSelectImage.java
private void btnSelectImageActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnSelectImageActionPerformed {//GEN-HEADEREND:event_btnSelectImageActionPerformed JFileChooser chooser = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter("Image files", "jpg", "png", "gif", "bmp"); chooser.setFileFilter(filter);//ww w. j a v a2s . c om chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); chooser.setDialogTitle("Select The Image"); chooser.setMultiSelectionEnabled(false); int res = chooser.showOpenDialog(null); if (res == JFileChooser.APPROVE_OPTION) { //Saving file inside the file File file = chooser.getSelectedFile(); // if(!file.equals(filter)) // { // JOptionPane.showMessageDialog(null, "Wrong File Selected","ERROR",JOptionPane.ERROR_MESSAGE); // return; // } //System.out.println(file.getAbsolutePath()); ImageIcon image = new ImageIcon(file.getAbsolutePath()); fileName = file.getAbsolutePath(); // Get Width And Height of PicLabel Rectangle rect = lblImage.getBounds(); //System.out.println(lblImage.getBounds()); //Scaling the image to fit in the picLabel Image scaledimage = image.getImage().getScaledInstance(rect.width, rect.height, Image.SCALE_DEFAULT); //converting the image back to image icon to make an acceptable picLabel image = new ImageIcon(scaledimage); lblImage.setIcon(image); txtPath.setText(fileName); try { File images = new File(fileName); FileInputStream fis = new FileInputStream(images); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; for (int readNum; (readNum = fis.read(buf)) != -1;) { bos.write(buf, 0, readNum); } person_image = bos.toByteArray(); } catch (Exception e) { JOptionPane.showMessageDialog(null, e); } } }
From source file:PeerPanel.java
private void saveLocationButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveLocationButtonActionPerformed JFileChooser jfc = new JFileChooser(); jfc.setCurrentDirectory(saveDirectory); jfc.setMultiSelectionEnabled(false); jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); if (jfc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { saveDirectory = jfc.getSelectedFile(); saveLocationDisplay.setText(saveDirectory.getAbsolutePath()); }/*from w ww . j a v a 2s . c om*/ }
From source file:com.simplexrepaginator.RepaginateFrame.java
protected JButton creatOutputButton() { JButton b = new JButton("Click or drag to set output file", PDF_1234); b.setHorizontalTextPosition(SwingConstants.LEFT); b.setIconTextGap(25);// w w w. j a va2s . c o m b.setTransferHandler(new OutputButtonTransferHandler()); b.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JFileChooser chooser = new JFileChooser(); chooser.setMultiSelectionEnabled(false); chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); if (chooser.showOpenDialog(RepaginateFrame.this) != JFileChooser.APPROVE_OPTION) return; repaginator.setOutputFiles(Arrays.asList(chooser.getSelectedFiles())); output.setText("<html><center>" + StringUtils.join(repaginator.getOutputFiles(), "<br>")); } }); return b; }
From source file:com.simplexrepaginator.RepaginateFrame.java
protected JButton createInputButton() { JButton b = new JButton("Click or drag to set input files", PDF_1342); b.setHorizontalTextPosition(SwingConstants.RIGHT); b.setIconTextGap(25);//from w ww . j ava 2 s . c o m b.setTransferHandler(new InputButtonTransferHandler()); b.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JFileChooser chooser = new JFileChooser(); chooser.setMultiSelectionEnabled(true); chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); if (chooser.showOpenDialog(RepaginateFrame.this) != JFileChooser.APPROVE_OPTION) return; setInput(Arrays.asList(chooser.getSelectedFiles())); if (JOptionPane.showConfirmDialog(RepaginateFrame.this, "Use input paths as output paths?", "Use Input As Output?", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) { setOutput(new ArrayList<File>(repaginator.getInputFiles())); } } }); return b; }
From source file:edu.harvard.mcz.imagecapture.jobs.JobCleanDirectory.java
private List<File> getFileList() { ArrayList<File> files = new ArrayList<File>(); String pathToCheck = ""; // Find the path in which to include files. File imagebase = null; // place to start the scan from, imagebase directory for SCAN_ALL File startPoint = null;//from w w w . ja va 2 s .c o m // If it isn't null, retrieve the image base directory from properties, and test for read access. if (Singleton.getSingletonInstance().getProperties().getProperties() .getProperty(ImageCaptureProperties.KEY_IMAGEBASE) == null) { JOptionPane.showMessageDialog(Singleton.getSingletonInstance().getMainFrame(), "Can't start scan. Don't know where images are stored. Set imagbase property.", "Can't Scan.", JOptionPane.ERROR_MESSAGE); } else { imagebase = new File(Singleton.getSingletonInstance().getProperties().getProperties() .getProperty(ImageCaptureProperties.KEY_IMAGEBASE)); if (imagebase != null) { if (imagebase.canRead()) { startPoint = imagebase; } else { // If it can't be read, null out imagebase imagebase = null; } } if (scan == SCAN_SPECIFIC && startPointSpecific != null && startPointSpecific.canRead()) { // A scan start point has been provided, don't launch a dialog. startPoint = startPointSpecific; } if (imagebase == null || scan == SCAN_SELECT) { // launch a file chooser dialog to select the directory to scan final JFileChooser fileChooser = new JFileChooser(); fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); if (scan == SCAN_SELECT && startPointSpecific != null && startPointSpecific.canRead()) { fileChooser.setCurrentDirectory(startPointSpecific); } else { if (Singleton.getSingletonInstance().getProperties().getProperties() .getProperty(ImageCaptureProperties.KEY_LASTPATH) != null) { fileChooser.setCurrentDirectory(new File(Singleton.getSingletonInstance().getProperties() .getProperties().getProperty(ImageCaptureProperties.KEY_LASTPATH))); } } int returnValue = fileChooser.showOpenDialog(Singleton.getSingletonInstance().getMainFrame()); if (returnValue == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); log.debug("Selected base directory: " + file.getName() + "."); startPoint = file; } else { //TODO: handle error condition log.error("Directory selection cancelled by user."); } } // Check that startPoint is or is within imagebase. if (!ImageCaptureProperties.isInPathBelowBase(startPoint)) { String base = Singleton.getSingletonInstance().getProperties().getProperties() .getProperty(ImageCaptureProperties.KEY_IMAGEBASE); log.error("Tried to scan directory (" + startPoint.getPath() + ") outside of base image directory (" + base + ")"); String message = "Can't scan and cleanup files outside of base image directory (" + base + ")"; JOptionPane.showMessageDialog(Singleton.getSingletonInstance().getMainFrame(), message, "Can't Scan outside image base directory.", JOptionPane.YES_NO_OPTION); } else if (ImageCaptureProperties.getPathBelowBase(startPoint).trim().length() == 0) { String base = Singleton.getSingletonInstance().getProperties().getProperties() .getProperty(ImageCaptureProperties.KEY_IMAGEBASE); log.error("Tried to scan directory (" + startPoint.getPath() + ") which is the base image directory."); String message = "Can only scan and cleanup files in a selected directory within the base directory (" + base + ").\nYou must select some subdirectory within the base directory to cleanup."; JOptionPane.showMessageDialog(Singleton.getSingletonInstance().getMainFrame(), message, "Can't Cleanup image base directory.", JOptionPane.YES_NO_OPTION); } else { if (!startPoint.canRead()) { JOptionPane.showMessageDialog(Singleton.getSingletonInstance().getMainFrame(), "Can't start scan. Unable to read selected directory: " + startPoint.getPath(), "Can't Scan.", JOptionPane.YES_NO_OPTION); } else { pathToCheck = ImageCaptureProperties.getPathBelowBase(startPoint); // retrieve a list of image records in the selected directory ICImageLifeCycle ils = new ICImageLifeCycle(); List<ICImage> images = ils.findAllInDir(pathToCheck); Iterator<ICImage> iter = images.iterator(); while (iter.hasNext()) { ICImage image = iter.next(); File imageFile = new File( ImageCaptureProperties.assemblePathWithBase(image.getPath(), image.getFilename())); files.add(imageFile); counter.incrementFilesSeen(); } } } } log.debug("Found " + files.size() + " Image files in directory to check."); return files; }