List of usage examples for javax.swing JFileChooser APPROVE_OPTION
int APPROVE_OPTION
To view the source code for javax.swing JFileChooser APPROVE_OPTION.
Click Source Link
From source file:Main.java
/** * Displays a {@link JFileChooser} to select a directory. * * @param title The title of the dialog. * @param startDirectory The directory where the dialog is initialed opened. * @return The {@link File} selected, returns null if no directory was selected. *///from w w w . j a va2 s . c o m public static File chooseDirectory(String title, File startDirectory) { JFileChooser chooser = new JFileChooser(); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); chooser.setDialogTitle(title); if (startDirectory != null) { chooser.setCurrentDirectory(startDirectory); } int status = chooser.showOpenDialog(null); if (status == JFileChooser.APPROVE_OPTION) { return chooser.getSelectedFile(); } return null; }
From source file:ActionListenerTest2.java
public void actionPerformed(ActionEvent e) { JFileChooser fileChooser = new JFileChooser(); int returnVal = fileChooser.showOpenDialog(null); if (returnVal == JFileChooser.APPROVE_OPTION) { System.out.print(fileChooser.getSelectedFile().getName()); }/* w w w.j a v a 2s . c o m*/ }
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 av a 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:net.rptools.maptool.util.AssetExtractor.java
public static void extract() throws Exception { new Thread() { @Override/* w w w .ja va 2 s. c om*/ 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(); }
From source file:Main.java
protected void saveToFile() { JFileChooser fileChooser = new JFileChooser(); int retval = fileChooser.showSaveDialog(save); if (retval == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); if (file == null) { return; }//from ww w .j av a2s .com if (!file.getName().toLowerCase().endsWith(".txt")) { file = new File(file.getParentFile(), file.getName() + ".txt"); } try { textArea.write(new OutputStreamWriter(new FileOutputStream(file), "utf-8")); Desktop.getDesktop().open(file); } catch (Exception e) { e.printStackTrace(); } } }
From source file:com.decypher.threadsclient.JPanelChart.java
private String getFolder() { JFileChooser folderPick = new JFileChooser(); folderPick.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); folderPick.setDialogType(JFileChooser.SAVE_DIALOG); folderPick.setMultiSelectionEnabled(false); String selected;/*ww w . ja v a 2 s . com*/ int returnVal = folderPick.showOpenDialog(null); if (returnVal == JFileChooser.APPROVE_OPTION) { selected = folderPick.getSelectedFile().getPath(); } else { selected = ""; } return selected; }
From source file:Main.java
/** * Displays the given file chooser. Utility method for avoiding of memory leak in JDK * 1.3 {@link javax.swing.JFileChooser#showDialog}. * * @param chooser the file chooser to display. * @param parent the parent window.//from w w w . j a v a2s . com * @param approveButtonText the text for the approve button. * * @return the return code of the chooser. */ public static final int showJFileChooser(JFileChooser chooser, Component parent, String approveButtonText) { if (approveButtonText != null) { chooser.setApproveButtonText(approveButtonText); chooser.setDialogType(javax.swing.JFileChooser.CUSTOM_DIALOG); } Frame frame = (parent instanceof Frame) ? (Frame) parent : (Frame) javax.swing.SwingUtilities.getAncestorOfClass(java.awt.Frame.class, parent); String title = chooser.getDialogTitle(); if (title == null) { title = chooser.getUI().getDialogTitle(chooser); } final JDialog dialog = new JDialog(frame, title, true); dialog.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); Container contentPane = dialog.getContentPane(); contentPane.setLayout(new BorderLayout()); contentPane.add(chooser, BorderLayout.CENTER); dialog.pack(); dialog.setLocationRelativeTo(parent); chooser.rescanCurrentDirectory(); final int[] retValue = new int[] { javax.swing.JFileChooser.CANCEL_OPTION }; ActionListener l = new ActionListener() { public void actionPerformed(ActionEvent ev) { if (ev.getActionCommand() == JFileChooser.APPROVE_SELECTION) { retValue[0] = JFileChooser.APPROVE_OPTION; } dialog.setVisible(false); dialog.dispose(); } }; chooser.addActionListener(l); dialog.show(); return (retValue[0]); }
From source file:Main.java
Main() { gui.setBorder(new EmptyBorder(2, 3, 2, 3)); String userDirLocation = System.getProperty("user.dir"); File userDir = new File(userDirLocation); // default to user directory fileChooser = new JFileChooser(userDir); Action open = new AbstractAction("Open") { @Override/* w ww. ja v a 2 s . c o m*/ public void actionPerformed(ActionEvent e) { int result = fileChooser.showOpenDialog(gui); if (result == JFileChooser.APPROVE_OPTION) { try { File f = fileChooser.getSelectedFile(); FileReader fr = new FileReader(f); output.read(fr, f); fr.close(); } catch (Exception ex) { ex.printStackTrace(); } } } }; Action save = new AbstractAction("Save") { @Override public void actionPerformed(ActionEvent e) { int result = fileChooser.showSaveDialog(gui); System.out.println("Not supported yet."); } }; JToolBar tb = new JToolBar(); gui.add(tb, BorderLayout.PAGE_START); tb.add(open); tb.add(save); gui.add(new JScrollPane(output)); }
From source file:Main.java
public void actionPerformed(ActionEvent e) { int retVal;//from www .j a v a 2s .c o m JFileChooser fc = new JFileChooser(); if (e.getActionCommand().equals("Open ...")) { fc.addChoosableFileFilter(new TextFilter()); retVal = fc.showOpenDialog(this); } else retVal = fc.showSaveDialog(this); if (retVal == JFileChooser.APPROVE_OPTION) System.out.println(fc.getSelectedFile().getName()); }
From source file:Main.java
/** * Consistent way to chosing a file to open with JFileChooser. * <p>/*from w w w . j a v a 2s .co m*/ * * @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; }