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:Main.java
/** * Consistent way to chosing multiple files to open with JFileChooser. * <p>/*from w ww . j a v a2 s.c om*/ * * @see JFileChooser#setFileSelectionMode(int) * @see #getSystemFiles(Component, int) * @param owner to show the component relative to. * @param mode selection mode for the JFileChooser. * @return File[] based on the user selection can be null. */ public static File[] getSystemFiles(Component owner, int mode, FileFilter[] filters) { JFileChooser jfc = new JFileChooser(); jfc.setFileSelectionMode(mode); jfc.setFileHidingEnabled(true); jfc.setMultiSelectionEnabled(true); jfc.setAcceptAllFileFilterUsed(true); if (filters != null) { for (int i = 0; i < filters.length; i++) { jfc.addChoosableFileFilter(filters[i]); } if (filters.length >= 1) { jfc.setFileFilter(filters[0]); } } int result = jfc.showOpenDialog(owner); if (result == JFileChooser.APPROVE_OPTION) { return jfc.getSelectedFiles(); } return new File[0]; }
From source file:Main.java
/** * Consistent way to chosing a file to open with JFileChooser. * <p>/*from w w w . j a va 2s . c om*/ * * @see JFileChooser#setFileSelectionMode(int) * @see #getSystemFiles(Component, int) * @param owner to show the component relative to. * @param mode selection mode for the JFileChooser. * @return File based on the user selection can be null. */ public static File getSystemFile(Component owner, int mode, FileFilter[] filters) { JFileChooser jfc = new JFileChooser(); jfc.setFileSelectionMode(mode); jfc.setFileHidingEnabled(true); jfc.setAcceptAllFileFilterUsed(true); if (filters != null) { for (int i = 0; i < filters.length; i++) { jfc.addChoosableFileFilter(filters[i]); } if (filters.length >= 1) { jfc.setFileFilter(filters[0]); } } int result = jfc.showOpenDialog(owner); if (result == JFileChooser.APPROVE_OPTION) { return jfc.getSelectedFile(); } return null; }
From source file:com.mgmtp.perfload.loadprofiles.ui.util.SwingUtils.java
public static JFileChooser createFileChooser(final File dir, final String description, final String extension) { JFileChooser fc = new JFileChooser(dir); fc.setFileSelectionMode(JFileChooser.FILES_ONLY); fc.setMultiSelectionEnabled(false);/*from w ww .j a v a 2s .c om*/ fc.setAcceptAllFileFilterUsed(false); fc.addChoosableFileFilter(new FileFilter() { @Override public String getDescription() { return description; } @Override public boolean accept(final File f) { return f.isDirectory() || FilenameUtils.isExtension(f.getName(), extension); } }); return fc; }
From source file:Main.java
/** * Displays a {@link JFileChooser} to select a file. * * @param title The title of the dialog. * @param startDirectory The directory where the dialog is initialed opened. * @param fileExtension File extension to filter each content of the opened * directories. Example ".xml".//from w w w. j a va 2s. c o m * @param startFile The preselected file where the dialog is initialed opened. * @return The {@link File} object selected, returns null if no file was selected. */ public static File chooseFile(String title, String startDirectory, final String fileExtension, String startFile) { JFileChooser chooser = new JFileChooser(); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); chooser.setDialogTitle(title); if (fileExtension != null && !fileExtension.trim().equals("")) { FileFilter filter = new FileFilter() { @Override public boolean accept(File pathname) { return pathname.isDirectory() || pathname.getName().endsWith(fileExtension); } @Override public String getDescription() { return "(" + fileExtension + ")"; } }; chooser.setFileFilter(filter); } if (startDirectory != null && !startDirectory.trim().equals("")) { chooser.setCurrentDirectory(new File(startDirectory)); } if (startFile != null && !startFile.trim().equals("")) { chooser.setSelectedFile(new File(startFile)); } int status = chooser.showOpenDialog(null); if (status == JFileChooser.APPROVE_OPTION) { return chooser.getSelectedFile(); } return null; }
From source file:Main.java
public static File chooseDir(Component parent, String title) { JFileChooser chooser = new JFileChooser(); // chooser.setCurrentDirectory(new java.io.File(".")); chooser.setDialogTitle(title);//from w w w . ja v a 2s . c o m chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); chooser.setApproveButtonText("Select Dir"); return chooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION ? chooser.getSelectedFile() : null; }
From source file:Main.java
public static JFileChooser createFileChooser(String title, FileFilter filter, int mode, boolean multiSelectionEnabled, int dialogType) { JFileChooser fileChooser = new JFileChooser(); fileChooser.setDialogTitle(title);// w w w .j a v a2 s . c om fileChooser.setFileFilter(filter); fileChooser.setFileSelectionMode(mode); fileChooser.setDialogType(dialogType); fileChooser.setMultiSelectionEnabled(multiSelectionEnabled); fileChooser.setCurrentDirectory(getLastDirectory()); return fileChooser; }
From source file:Main.java
public static String browseForFile(Window owner, String file, int selectionMode, String title, FileFilter filter) {/*from w ww . j a va 2 s . co m*/ final String curDir = System.getProperty("user.dir"); JFileChooser chooser = new JFileChooser(curDir); chooser.setDialogType(JFileChooser.OPEN_DIALOG); chooser.setFileSelectionMode(selectionMode); chooser.setApproveButtonText("Select"); chooser.setApproveButtonMnemonic('s'); chooser.setDialogTitle(title); if (filter != null) chooser.setFileFilter(filter); if (file != null && !file.isEmpty()) { File curFile = new File(file); chooser.setCurrentDirectory(curFile.getAbsoluteFile().getParentFile()); if (curFile.isDirectory()) { try { chooser.setSelectedFile(curFile.getCanonicalFile()); } catch (IOException ex) { } } else { chooser.setSelectedFile(curFile); } } if (chooser.showOpenDialog(owner) == JFileChooser.APPROVE_OPTION) { String path = chooser.getSelectedFile().getPath(); try { path = new File(path).getCanonicalPath(); } catch (IOException e) { } // make path relative if possible if (path.startsWith(curDir)) { path = "." + path.substring(curDir.length()); } return path; } return null; }
From source file:com.floreantpos.bo.actions.DataExportAction.java
public static JFileChooser getFileChooser() { JFileChooser fileChooser = new JFileChooser(); fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); fileChooser.setMultiSelectionEnabled(false); fileChooser.setSelectedFile(new File("floreantpos-menu-items.xml")); //$NON-NLS-1$ fileChooser.setFileFilter(new FileFilter() { @Override// ww w .j a v a 2 s . co m public String getDescription() { return "XML File"; //$NON-NLS-1$ } @Override public boolean accept(File f) { if (f.getName().endsWith(".xml")) { //$NON-NLS-1$ return true; } return false; } }); return fileChooser; }
From source file:Main.java
/** * Creates a new JFileChooser and returns the selected path's location. * @param title/*from ww w .ja v a 2s . c o m*/ * @param openTo * @param chooseDirectory * @param filter * @return */ public static String getFilePath(String title, String openTo, boolean chooseDirectory, FileFilter filter) { String location = null; JFileChooser chooser = new JFileChooser(title); chooser.setCurrentDirectory(new File(openTo)); chooser.setFileFilter(filter); chooser.setDialogTitle("Open Cache"); if (chooseDirectory) { chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); } else { chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); } chooser.setAcceptAllFileFilterUsed(false); if (chooser.showOpenDialog(chooser) == JFileChooser.APPROVE_OPTION) { location = chooser.getSelectedFile().getAbsolutePath(); } return location; }
From source file:net.rptools.maptool.util.AssetExtractor.java
public static void extract() throws Exception { new Thread() { @Override// w ww. j av a 2 s . c o m public void run() { JFileChooser chooser = new JFileChooser(); chooser.setMultiSelectionEnabled(false); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); if (chooser.showOpenDialog(null) != JFileChooser.APPROVE_OPTION) { return; } File file = chooser.getSelectedFile(); File newDir = new File(file.getParentFile(), file.getName().substring(0, file.getName().lastIndexOf('.')) + "_images"); JLabel label = new JLabel("", JLabel.CENTER); JFrame frame = new JFrame(); frame.setTitle("Campaign Image Extractor"); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setSize(400, 75); frame.add(label); SwingUtil.centerOnScreen(frame); frame.setVisible(true); Reader r = null; OutputStream out = null; PackedFile pakfile = null; try { newDir.mkdirs(); label.setText("Loading campaign ..."); pakfile = new PackedFile(file); Set<String> files = pakfile.getPaths(); XStream xstream = new XStream(); int count = 0; for (String filename : files) { count++; if (filename.indexOf("assets") < 0) { continue; } r = pakfile.getFileAsReader(filename); Asset asset = (Asset) xstream.fromXML(r); IOUtils.closeQuietly(r); File newFile = new File(newDir, asset.getName() + ".jpg"); label.setText("Extracting image " + count + " of " + files.size() + ": " + newFile); if (newFile.exists()) { newFile.delete(); } newFile.createNewFile(); out = new FileOutputStream(newFile); FileUtil.copyWithClose(new ByteArrayInputStream(asset.getImage()), out); } label.setText("Done."); } catch (Exception ioe) { MapTool.showInformation("AssetExtractor failure", ioe); } finally { if (pakfile != null) pakfile.close(); IOUtils.closeQuietly(r); IOUtils.closeQuietly(out); } } }.start(); }