Java Code Examples for javax.swing.JFileChooser#showDialog()

The following examples show how to use javax.swing.JFileChooser#showDialog() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainFrame.java    From mpcmaid with GNU Lesser General Public License v2.1 6 votes vote down vote up
protected void batchCreate() {
	final JFileChooser batchDialog = new JFileChooser(Preferences.getInstance().getSavePath());
	batchDialog.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

	// final FileDialog saveDialog = new FileDialog(this);
	batchDialog.setDialogType(JFileChooser.OPEN_DIALOG);
	batchDialog.setDialogTitle("Choose the base directory to browse recursively");

	batchDialog.showDialog(this, "Batch Create Program");
	final File dir = batchDialog.getSelectedFile();
	if (dir != null) {
		try {
			programEditor.batchCreate(dir);
		} catch (Exception e) {
			e.printStackTrace();// error occurred
		}
	}
}
 
Example 2
Source File: FileNameComponent.java    From mzmine2 with GNU General Public License v2.0 6 votes vote down vote up
public void actionPerformed(ActionEvent e) {
  JFileChooser fileChooser = new JFileChooser();
  fileChooser.setMultiSelectionEnabled(false);

  String currentPath = txtFilename.getText();
  if (currentPath.length() > 0) {
    File currentFile = new File(currentPath);
    File currentDir = currentFile.getParentFile();
    if (currentDir != null && currentDir.exists())
      fileChooser.setCurrentDirectory(currentDir);
  }

  int returnVal = fileChooser.showDialog(MZmineCore.getDesktop().getMainWindow(), "Select file");

  if (returnVal == JFileChooser.APPROVE_OPTION) {
    String selectedPath = fileChooser.getSelectedFile().getAbsolutePath();
    txtFilename.setText(selectedPath);
  }
}
 
Example 3
Source File: DExportKeyPair.java    From keystore-explorer with GNU General Public License v3.0 6 votes vote down vote up
private void browsePressed() {
	JFileChooser chooser = FileChooserFactory.getPkcs12FileChooser();

	File currentExportFile = new File(jtfExportFile.getText().trim());

	if ((currentExportFile.getParentFile() != null) && (currentExportFile.getParentFile().exists())) {
		chooser.setCurrentDirectory(currentExportFile.getParentFile());
		chooser.setSelectedFile(currentExportFile);
	} else {
		chooser.setCurrentDirectory(CurrentDirectory.get());
	}

	chooser.setDialogTitle(res.getString("DExportKeyPair.ChooseExportFile.Title"));
	chooser.setMultiSelectionEnabled(false);

	int rtnValue = JavaFXFileChooser.isFxAvailable() ? chooser.showSaveDialog(this)
			: chooser.showDialog(this, res.getString("DExportKeyPair.ChooseExportFile.button"));
	if (rtnValue == JFileChooser.APPROVE_OPTION) {
		File chosenFile = chooser.getSelectedFile();
		CurrentDirectory.updateForFile(chosenFile);
		jtfExportFile.setText(chosenFile.toString());
		jtfExportFile.setCaretPosition(0);
	}
}
 
Example 4
Source File: ProductSetPanel.java    From snap-desktop with GNU General Public License v3.0 6 votes vote down vote up
private static File[] getFilePath(Component component, String title) {

        File[] files = null;
        final File openDir = new File(SnapApp.getDefault().getPreferences().
                get(OpenProductAction.PREFERENCES_KEY_LAST_PRODUCT_DIR, "."));
        final JFileChooser chooser = FileChooserFactory.getInstance().createFileChooser(openDir);
        chooser.setMultiSelectionEnabled(true);
        chooser.setDialogTitle(title);
        if (chooser.showDialog(component, "OK") == JFileChooser.APPROVE_OPTION) {
            files = chooser.getSelectedFiles();

            SnapApp.getDefault().getPreferences().
                    put(OpenProductAction.PREFERENCES_KEY_LAST_PRODUCT_DIR, chooser.getCurrentDirectory().getAbsolutePath());
        }
        return files;
    }
 
Example 5
Source File: EditLogControlPanel.java    From VanetSim with GNU General Public License v3.0 6 votes vote down vote up
public void setFilePath(JFormattedTextField textField){
	//begin with creation of new file
	JFileChooser fc = new JFileChooser();
	//set directory and ".log" filter
	fc.setCurrentDirectory(new File(System.getProperty("user.dir")));
	fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
	fc.setFileFilter(logFileFilter_);
	
	int status = fc.showDialog(this, Messages.getString("EditLogControlPanel.approveButton"));
	
	if(status == JFileChooser.APPROVE_OPTION){
			textField.setValue(fc.getSelectedFile().getAbsolutePath());
			textField.setToolTipText(fc.getSelectedFile().getAbsolutePath());
			textField.setCaretPosition(textField.getText().length());
	}
}
 
Example 6
Source File: DOpenPkcs11KeyStore.java    From keystore-explorer with GNU General Public License v3.0 6 votes vote down vote up
private void browsePressed() {
	JFileChooser chooser = FileChooserFactory.getLibFileChooser();

	File currentLibFile = new File(((String) jtfP11Library.getSelectedItem()).trim());

	if (currentLibFile.getParentFile() != null && currentLibFile.getParentFile().exists()) {
		chooser.setCurrentDirectory(currentLibFile.getParentFile());
		chooser.setSelectedFile(currentLibFile);
	} else {
		chooser.setCurrentDirectory(CurrentDirectory.get());
	}

	chooser.setDialogTitle(res.getString("DOpenPkcs11KeyStore.SelectLib.Title"));
	chooser.setMultiSelectionEnabled(false);

	int rtnValue = chooser.showDialog(this, res.getString("DOpenPkcs11KeyStore.SelectLib.button"));
	if (rtnValue == JFileChooser.APPROVE_OPTION) {
		File chosenFile = chooser.getSelectedFile();
		CurrentDirectory.updateForFile(chosenFile);
		String libPath = chosenFile.toString();
		jtfP11Library.addItem(libPath);
		jtfP11Library.setSelectedItem(libPath);
	}
}
 
Example 7
Source File: SwingAppleCommander.java    From AppleCommander with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Open a file.
 */
protected void openFile() {
	JFileChooser jc = new JFileChooser();
	String pathName = userPreferences.getDiskImageDirectory();
	if (null == pathName) {
		pathName = ""; //$NON-NLS-1$
		}
	jc.setCurrentDirectory(new File(pathName));
	EmulatorFileFilter ff = new EmulatorFileFilter();
	jc.setFileFilter(ff);
	int rc = jc.showDialog(this, textBundle.get("Open")); //$NON-NLS-1$
	if (rc == 0) {
		userPreferences.setDiskImageDirectory(jc.getSelectedFile().getParent());
		UserPreferences.getInstance().save();
		addDiskExplorerTab(jc.getSelectedFile());
	}
}
 
Example 8
Source File: DImportKeyPairPvk.java    From keystore-explorer with GNU General Public License v3.0 6 votes vote down vote up
private void certificateBrowsePressed() {
	JFileChooser chooser = FileChooserFactory.getCertFileChooser();

	File currentFile = new File(jtfCertificatePath.getText());

	if ((currentFile.getParentFile() != null) && (currentFile.getParentFile().exists())) {
		chooser.setCurrentDirectory(currentFile.getParentFile());
	} else {
		chooser.setCurrentDirectory(CurrentDirectory.get());
	}

	chooser.setDialogTitle(res.getString("DImportKeyPairPvk.ChooseCertificate.Title"));

	chooser.setMultiSelectionEnabled(false);

	int rtnValue = chooser.showDialog(this, res.getString("DImportKeyPairPvk.CertificateFileChooser.button"));
	if (rtnValue == JFileChooser.APPROVE_OPTION) {
		File chosenFile = chooser.getSelectedFile();
		CurrentDirectory.updateForFile(chosenFile);
		jtfCertificatePath.setText(chosenFile.toString());
		jtfCertificatePath.setCaretPosition(0);
	}
}
 
Example 9
Source File: FileOperations.java    From opt4j with MIT License 5 votes vote down vote up
/**
 * Opens the file chooser for loading the current configuration file.
 */
public void load() {
	JFileChooser fc = fileChooser.get();
	fc.setFileFilter(null);
	fc.setVisible(true);
	fc.setCurrentDirectory(getFile());

	int returnVal = fc.showDialog(null, "Load");

	if (returnVal == JFileChooser.APPROVE_OPTION) {
		load(fc.getSelectedFile());
	}
}
 
Example 10
Source File: WhiteRabbitMain.java    From WhiteRabbit with Apache License 2.0 5 votes vote down vote up
private void pickFolder() {
	JFileChooser fileChooser = new JFileChooser(new File(folderField.getText()));
	fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
	int returnVal = fileChooser.showDialog(frame, "Select folder");
	if (returnVal == JFileChooser.APPROVE_OPTION) {
		File selectedDirectory = fileChooser.getSelectedFile();
		if (!selectedDirectory.exists()) {
			// When no directory is selected when approving, FileChooser incorrectly appends the current directory to the path.
			// Take the opened directory instead.
			selectedDirectory = fileChooser.getCurrentDirectory();
		}
		folderField.setText(selectedDirectory.getAbsolutePath());
	}
}
 
Example 11
Source File: PickMIDlet.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
private void selectFile(JTextField field, String type) {
    JFileChooser c = ResourceEditorView.createFileChooser("*" + type, type);
    if(JFileChooser.APPROVE_OPTION == c.showDialog(this, "Select")) {
        File f = c.getSelectedFile();
        field.setText(f.getAbsolutePath());
        Preferences.userNodeForPackage(ResourceEditorView.class).put("lastDir", f.getParentFile().getAbsolutePath());
    }
    updateMIDletList();
    if(midletPicker.getModel().getSize() > 0) {
        midletPicker.setSelectedIndex(0);
    }
    updateOKEnabled();
}
 
Example 12
Source File: DialogProviderManager.java    From netbeans-mmd-plugin with Apache License 2.0 5 votes vote down vote up
@Override
@Nullable
public File msgSaveFileDialog(@Nullable final Component parentComponent, @Nonnull final String id, @Nonnull final String title, @Nullable final File defaultFolder, final boolean filesOnly, @Nonnull @MustNotContainNull final FileFilter[] fileFilters, @Nonnull final String approveButtonText) {
  final File folderToUse;
  if (defaultFolder == null) {
    folderToUse = cacheSaveFileThroughDialog.find(null, id);
  } else {
    folderToUse = defaultFolder;
  }

  final JFileChooser fileChooser = new JFileChooser(folderToUse);
  fileChooser.setDialogType(JFileChooser.SAVE_DIALOG);
  fileChooser.setDialogTitle(title);
  fileChooser.setApproveButtonText(approveButtonText);
  fileChooser.setAcceptAllFileFilterUsed(true);
  for (final FileFilter f : fileFilters) {
    fileChooser.addChoosableFileFilter(f);
  }
  if (fileFilters.length != 0) {
    fileChooser.setFileFilter(fileFilters[0]);
  }
  fileChooser.setMultiSelectionEnabled(false);

  File result = null;
  if (fileChooser.showDialog(GetUtils.ensureNonNull(
      parentComponent == null ? null : SwingUtilities.windowForComponent(parentComponent),
      Main.getApplicationFrame()),
      approveButtonText) == JFileChooser.APPROVE_OPTION
  ) {
    result = cacheSaveFileThroughDialog.put(id, fileChooser.getSelectedFile());
  }

  return result;
}
 
Example 13
Source File: WhiteRabbitMain.java    From WhiteRabbit with Apache License 2.0 5 votes vote down vote up
private void pickScanReportFile() {
	JFileChooser fileChooser = new JFileChooser(new File(folderField.getText()));
	fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
	int returnVal = fileChooser.showDialog(frame, "Select scan report file");
	if (returnVal == JFileChooser.APPROVE_OPTION)
		scanReportFileField.setText(fileChooser.getSelectedFile().getAbsolutePath());
}
 
Example 14
Source File: FileChooser.java    From EdgeSim with MIT License 5 votes vote down vote up
public void actionPerformed(ActionEvent e) {  
    JFileChooser jfc=new JFileChooser();  
    jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );  
    jfc.showDialog(new JLabel(), "Select");  
    File file=jfc.getSelectedFile();  
    if(file.isDirectory()){  
    	logger.debug("Folder:"+file.getAbsolutePath());  
    }else if(file.isFile()){  
        System.out.println("File:"+file.getAbsolutePath());  
    }  
    	logger.debug(jfc.getSelectedFile().getName());  
      
}
 
Example 15
Source File: Hk2Cookie.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Deploy module from directory on GlassFish server.
 * <p/>
 * @return Result of undeploy task execution.
 */
@Override
public Future<ResultString> deployDirectory() {
    JFileChooser chooser = new JFileChooser();
    chooser.setDialogTitle(NbBundle.getMessage(Hk2ItemNode.class,
            "LBL_ChooseButton"));
    chooser.setDialogType(JFileChooser.CUSTOM_DIALOG);
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    chooser.setMultiSelectionEnabled(false);

    int returnValue = chooser.showDialog(WindowManager.getDefault()
            .getMainWindow(), NbBundle.getMessage(
            Hk2ItemNode.class, "LBL_ChooseButton"));
    if (instance != null
            || returnValue != JFileChooser.APPROVE_OPTION) {
        return null;
    }

    final File dir
            = new File(chooser.getSelectedFile().getAbsolutePath());

    Future<ResultString> future = ServerAdmin.<ResultString>exec(
            instance, new CommandDeploy(dir.getParentFile().getName(),
            Util.computeTarget(instance.getProperties()),
            dir, null, null, null));
    status = new WeakReference<Future<ResultString>>(future);
    return future;
}
 
Example 16
Source File: AntCustomizer.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void actionPerformed (ActionEvent e) {
    if (!listen) return;
    Object o = e.getSource ();
    if (o == cbAlwaysShowOutput) {
        fireChanged();
    } else
    if (o == cbReuseOutput) {
        fireChanged();
    } else
    if (o == cbSaveFiles) {
        fireChanged();
    } else
    if (o == cbVerbosity) {
        fireChanged();
    } else
    if (o == bAntHome) {
        JFileChooser chooser = new JFileChooser (tfAntHome.getText ());
        chooser.setFileSelectionMode (JFileChooser.DIRECTORIES_ONLY);
        int r = chooser.showDialog (
            SwingUtilities.getWindowAncestor (this),
            NbBundle.getMessage(AntCustomizer.class, "Select_Directory")
        );
        if (r == JFileChooser.APPROVE_OPTION) {
            File file = chooser.getSelectedFile ();
            if (!new File (new File (file, "lib"), "ant.jar").isFile ()) {
                DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(
                    NbBundle.getMessage(AntCustomizer.class, "Not_a_ant_home", file),
                    NotifyDescriptor.Message.WARNING_MESSAGE
                ));
                return;
            }
            tfAntHome.setText (file.getAbsolutePath ());
            AntSettings.setAntHome(file);
            updateAntVersion();
            fireChanged();
        }
    }
}
 
Example 17
Source File: BiomeExporterDialog.java    From amidst with GNU General Public License v3.0 5 votes vote down vote up
private Path getExportPath() {
	Path file = null;

	JFileChooser fileChooser = new JFileChooser();
	fileChooser.setFileFilter(new PNGFileFilter());
	fileChooser.setAcceptAllFileFilterUsed(false);
	fileChooser.setSelectedFile(Paths.get(pathField.getText()).toAbsolutePath().toFile());
	if (fileChooser.showDialog(dialog, "Confirm") == JFileChooser.APPROVE_OPTION) {
		file = Actions.appendFileExtensionIfNecessary(fileChooser.getSelectedFile().toPath(), "png");
	}

	return file;
}
 
Example 18
Source File: FileChooser.java    From CQL with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 *
 *
 * @param title
 * @param mode
 * @param filter
 *
 * @return
 */
private static File chooseFileSwing(String title, Mode mode, FileFilter filter, String defaultName) {
	Easik e = Easik.getInstance();
	EasikSettings s = e.getSettings();
	JFileChooser dialog = new JFileChooser(s.getDefaultFolder());

	if (filter != null) {
		dialog.setFileFilter(filter);
	}

	int result;

	if (mode == Mode.DIRECTORY) {
		dialog.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

		result = dialog.showDialog(e.getFrame(), "Select folder");
	} else if (mode == Mode.SAVE) {
		if (defaultName != null) {
			dialog.setSelectedFile(new File(defaultName));
		}
		result = dialog.showSaveDialog(e.getFrame());
	} else {
		result = dialog.showOpenDialog(e.getFrame());
	}

	if (result != JFileChooser.APPROVE_OPTION) {
		return null;
	}

	File selFile = dialog.getSelectedFile();

	if (mode != Mode.DIRECTORY) {
		s.setProperty("folder_last", selFile.getParentFile().getAbsolutePath());
	}

	return selFile;
}
 
Example 19
Source File: LaunchSaver.java    From osp with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Selects the tabset with a chooser.
 */
private void chooseTabSet() {
  String fileName = XML.getResolvedPath(builder.tabSetName, Launcher.tabSetBasePath);
  JFileChooser chooser = Launcher.getXMLChooser();
  chooser.setSelectedFile(new File(fileName));
  chooser.setFileFilter(Launcher.xsetFileFilter);
  int result = chooser.showDialog(this, LaunchRes.getString("Saver.FileChooser.Title")); //$NON-NLS-1$
  if(result==JFileChooser.APPROVE_OPTION) {
    File file = chooser.getSelectedFile();
    fileName = file.getAbsolutePath();
    setTabSetName(fileName);
    setTabSetBasePath(XML.getDirectoryPath(fileName));
  }
  refresh();
}
 
Example 20
Source File: PackageSelectorGUI.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Opens dialog window to select desired PEAR package file for a given component.
 * 
 * @param componentId
 *          The given component ID.
 * @return Selected PEAR package file for the given component, or <code>null</code>, if the
 *         selection cancelled.
 */
public synchronized File selectPackageFile(String componentId) {
  // get last package file path
  Preferences userPrefs = Preferences.userNodeForPackage(getClass());
  String lastFilePath = (userPrefs != null) ? userPrefs.get(LAST_PACKAGE_FILE_KEY, "") : "";
  File lastFile = lastFilePath.length() > 0 ? new File(lastFilePath) : null;
  File lastDir = lastFile != null ? lastFile.getParentFile() : new File(".");
  // create JFileChooser
  JFileChooser fileChooser = new JFileChooser();
  fileChooser.addChoosableFileFilter(new PackageFileFilter());
  fileChooser.setCurrentDirectory(lastDir);
  fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
  fileChooser.setDialogTitle("Select " + componentId + " PEAR file");
  // show information dialog
  String message = "Select the PEAR file of the\n" + "\n" + componentId + "\n"
          + "\ncomponent using the following file dialog.";
  JOptionPane.showMessageDialog(_dialogFrame, message);
  // open dialog window
  File selectedFile = null;
  int result = fileChooser.showDialog(_dialogFrame, "Select");
  if (result == JFileChooser.APPROVE_OPTION) {
    // set 'selected file'
    selectedFile = fileChooser.getSelectedFile();
    if (selectedFile != null) {
      // set 'last file' in user prefs
      userPrefs.put(LAST_PACKAGE_FILE_KEY, selectedFile.getAbsolutePath());
    }
  }
  return selectedFile;
}