下面列出了javax.swing.JFileChooser#APPROVE_OPTION 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void actionPerformed(ActionEvent actionEvent) {
int returnVal = fileChooser.showOpenDialog(frame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
Path proxyFile = fileChooser.getSelectedFile().toPath();
LambdaAttack.getLogger().log(Level.INFO, "Opening: {0}.", proxyFile.getFileName());
botManager.getThreadPool().submit(() -> {
try {
List<String> names = Files.lines(proxyFile).distinct().collect(Collectors.toList());
LambdaAttack.getLogger().log(Level.INFO, "Loaded {0} names", names.size());
botManager.setNames(names);
} catch (Exception ex) {
LambdaAttack.getLogger().log(Level.SEVERE, null, ex);
}
});
}
}
/**
* Browse dir.
*
* @param f the f
*/
void browseDir(JTextArea f) {
String startingDir = f.getText();
if (startingDir.length() == 0) {
// default to user.dir
startingDir = System.getProperty("user.dir");
}
JFileChooser c = new JFileChooser(startingDir);
c.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = c.showOpenDialog(gui);
if (returnVal == JFileChooser.APPROVE_OPTION) {
try {
f.setText(c.getSelectedFile().getCanonicalPath());
Prefs.set(gui);
} catch (Exception e) { // do nothing
}
}
}
private void browseProjectFolderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseProjectFolderActionPerformed
JFileChooser chooser = new JFileChooser();
FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
chooser.setFileSelectionMode (JFileChooser.DIRECTORIES_ONLY);
if (projectFolder.getText().length() > 0 && getProjectFolder().exists()) {
chooser.setSelectedFile(getProjectFolder());
} else if (projectLocation.getText().length() > 0 && getProjectLocation().exists()) {
chooser.setSelectedFile(getProjectLocation());
} else {
chooser.setSelectedFile(ProjectChooser.getProjectsFolder());
}
chooser.setDialogTitle(NbBundle.getMessage(BasicProjectInfoPanel.class, "LBL_Browse_Project_Folder")); //NOI18N
if ( JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) {
File projectDir = FileUtil.normalizeFile(chooser.getSelectedFile());
projectFolder.setText(projectDir.getAbsolutePath());
}
}
private File chooseFileForOpen(final String title, final File initial,
final AtomicReference<FileFilter> selectedFilter,
final FileFilter... filter) {
final JFileChooser chooser = new JFileChooser(initial);
for (final FileFilter f : filter) {
chooser.addChoosableFileFilter(f);
}
chooser.setAcceptAllFileFilterUsed(false);
chooser.setMultiSelectionEnabled(false);
chooser.setDialogTitle(title);
chooser.setFileFilter(filter[0]);
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
final File result;
if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
result = chooser.getSelectedFile();
if (selectedFilter != null) {
selectedFilter.set(chooser.getFileFilter());
}
} else {
result = null;
}
return result;
}
public File saveWorkflow() {
try {
JFileChooser fileChooser = this.getWorkflowFileChooser();
int ret = fileChooser.showSaveDialog(this.getMainFrame());
if (ret == JFileChooser.APPROVE_OPTION) {
File selected = fileChooser.getSelectedFile();
File newFile = selected.getName().endsWith(WorkflowManager.SCRIPT_EXTENSION) ? selected : new File(selected.getCanonicalPath() + "." + WorkflowManager.SCRIPT_EXTENSION);
super.saveWorkflow(newFile);
menuBar.addRecentWorkflow(newFile.getName(), Files.toUrl(newFile));
menuBar.updateMenuStatus();
return newFile;
}
menuBar.updateMenuStatus();
} catch (IOException e) {
reportException(e);
}
return null;
}
private void javadocBrowseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_javadocBrowseActionPerformed
JFileChooser chooser = new JFileChooser();
FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
chooser.setFileSelectionMode (JFileChooser.FILES_AND_DIRECTORIES);
chooser.setMultiSelectionEnabled(false);
if (lastChosenFile != null) {
chooser.setSelectedFile(lastChosenFile);
} else if (javadoc.getText().length() > 0) {
chooser.setSelectedFile(new File(javadoc.getText()));
} else {
File files[] = model.getBaseFolder().listFiles();
if (files != null && files.length > 0) {
chooser.setSelectedFile(files[0]);
} else {
chooser.setSelectedFile(model.getBaseFolder());
}
}
chooser.setDialogTitle(NbBundle.getMessage(OutputPanel.class, "LBL_Browse_Javadoc")); // NOI18N
if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) {
File file = chooser.getSelectedFile();
file = FileUtil.normalizeFile(file);
javadoc.setText(file.getAbsolutePath());
lastChosenFile = file;
}
}
/**
* Handles the pressing of a pathLbl button: display the file chooser
* and update the path if a file is selected
*
* @param pathIdx
*/
private void chooseFile(int pathIdx) {
int returnVal = fileChooser.showOpenDialog(this);
// Did the user choose valid input?
if ((returnVal != JFileChooser.APPROVE_OPTION)
|| (fileChooser.getSelectedFile() == null)) {
return;
}
// Get relative path
String relativePath = Configuration.widgetsDir().toURI()
.relativize(fileChooser.getSelectedFile().toURI())
.getPath();
// Set text
path.get(pathIdx).getDocument().removeDocumentListener(this);
path.get(pathIdx).setText(relativePath);
path.get(pathIdx).getDocument().addDocumentListener(this);
}
private void addClasspathActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addClasspathActionPerformed
FileChooser chooser;
chooser = new FileChooser(model.getBaseFolder(), null);
FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
chooser.setFileSelectionMode (JFileChooser.FILES_AND_DIRECTORIES);
chooser.setMultiSelectionEnabled(true);
chooser.setDialogTitle(NbBundle.getMessage(ClasspathPanel.class, "LBL_Browse_Classpath"));
if (lastChosenFile != null) {
chooser.setCurrentDirectory(lastChosenFile);
} else {
chooser.setCurrentDirectory(model.getBaseFolder());
}
//#65354: prevent adding a non-folder element on the classpath:
FileFilter fileFilter = new SimpleFileFilter (
NbBundle.getMessage( ClasspathPanel.class, "LBL_ZipJarFolderFilter" ), // NOI18N
new String[] {"ZIP","JAR"} ); // NOI18N
//#61789 on old macosx (jdk 1.4.1) these two method need to be called in this order.
chooser.setAcceptAllFileFilterUsed( false );
chooser.setFileFilter(fileFilter);
if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) {
String[] filePaths = null;
try {
filePaths = chooser.getSelectedPaths();
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
for (String filePath : filePaths) {
listModel.addElement(filePath);
lastChosenFile = chooser.getCurrentDirectory();
}
applyChanges();
updateButtons();
}
}
/**
* Pops up a "Save File" file chooser dialog and takes user through process of saving and object to a file.
*
* @param object the object that will be converted to a string and saved
* @param parent the parent component of the dialog, can be <code>null</code>;
* see <code>showDialog</code> in class JFileChooser for details
*/
public static void saveToFile(Object object, Component parent) {
JFileChooser fileChooser = new JFileChooser(new File(OSPRuntime.chooserDir));
FontSizer.setFonts(chooser, FontSizer.getLevel());
int result = fileChooser.showSaveDialog(parent);
if(result!=JFileChooser.APPROVE_OPTION) {
return;
}
File file = fileChooser.getSelectedFile();
if(file.exists()) {
int selected = JOptionPane.showConfirmDialog(parent, ControlsRes.getString("OSPLog.ReplaceExisting_dialog_message")+" "+file.getName()+ControlsRes.getString("OSPLog.question_mark"), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
ControlsRes.getString("OSPLog.ReplaceFile_dialog_title"), //$NON-NLS-1$
JOptionPane.YES_NO_CANCEL_OPTION);
if(selected!=JOptionPane.YES_OPTION) {
return;
}
}
try {
FileWriter fw = new FileWriter(file);
PrintWriter pw = new PrintWriter(fw);
pw.print(object.toString());
pw.close();
} catch(IOException e) {
JOptionPane.showMessageDialog(parent, ControlsRes.getString("Dialog.SaveError.Message"), //$NON-NLS-1$
ControlsRes.getString("Dialog.SaveError.Title"), //$NON-NLS-1$
JOptionPane.ERROR_MESSAGE);
}
}
@Override
public void selectFolder(final Runnable callback) {
final boolean save = false;
JApplet applet = new JApplet(); // TODO fail safe
final JFileChooser fc = new JFileChooser(new File(path));
fc.setFileFilter(new FileFilter() {
@Override
public String getDescription() {
return "Folder";
}
@Override
public boolean accept(File f) {
return f.isDirectory();
}
});
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int r = save ? fc.showSaveDialog(applet) : fc.showOpenDialog(applet);
if(r == JFileChooser.APPROVE_OPTION){
final File file = fc.getSelectedFile();
path = file.getPath();
Gdx.app.postRunnable(new Runnable() {
@Override
public void run() {
lastFile = Gdx.files.absolute(file.getAbsolutePath());
callback.run();
}
});
}else{
// callback.cancel();
}
applet.destroy();
}
public void actionPerformed(ActionEvent e) {
try {
_fileChooser.setFileFilter(_filter);
_fileChooser.setSelectedFile(new File(""));
if (_fileChooser.showOpenDialog(MainFrame.getInstance())
== JFileChooser.APPROVE_OPTION) {
JarFile jar = new JarFile(_fileChooser.getSelectedFile());
if (jar.getManifest() == null) {
jar.close();
MainFrame.getInstance().info(Messages.getString("noManifest"));
return;
}
Attributes attr = jar.getManifest().getMainAttributes();
String mainClass = (String) attr.getValue("Main-Class");
String classPath = (String) attr.getValue("Class-Path");
jar.close();
_mainclassField.setText(mainClass != null ? mainClass : "");
DefaultListModel model = new DefaultListModel();
if (classPath != null) {
String[] paths = classPath.split(" ");
for (int i = 0; i < paths.length; i++) {
model.addElement(paths[i]);
}
}
_classpathList.setModel(model);
}
} catch (IOException ex) {
MainFrame.getInstance().warn(ex.getMessage());
}
}
public void saveXML() {
JFileChooser chooser = OSPRuntime.getChooser();
if(chooser==null) {
return;
}
String oldTitle = chooser.getDialogTitle();
chooser.setDialogTitle(ControlsRes.getString("ControlFrame.Save_XML_Data")); //$NON-NLS-1$
int result = chooser.showSaveDialog(null);
chooser.setDialogTitle(oldTitle);
if(result==JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
// check to see if file already exists
if(file.exists()) {
int selected = JOptionPane.showConfirmDialog(null, ControlsRes.getString("ControlFrame.Replace_existing")+file.getName() //$NON-NLS-1$
+ControlsRes.getString("ControlFrame.question_mark"), ControlsRes.getString("ControlFrame.Replace_File"), //$NON-NLS-1$ //$NON-NLS-2$
JOptionPane.YES_NO_CANCEL_OPTION);
if(selected!=JOptionPane.YES_OPTION) {
return;
}
}
OSPRuntime.chooserDir = chooser.getCurrentDirectory().toString();
String fileName = file.getAbsolutePath();
// String fileName = XML.getRelativePath(file.getAbsolutePath());
if((fileName==null)||fileName.trim().equals("")) { //$NON-NLS-1$
return;
}
int i = fileName.toLowerCase().lastIndexOf(".xml"); //$NON-NLS-1$
if(i!=fileName.length()-4) {
fileName += ".xml"; //$NON-NLS-1$
}
XMLControl xml = new XMLControlElement(getOSPApp());
xml.write(fileName);
}
}
/**
* Uses a JFileChooser to ask for a name.
* @param chooser JFileChooser
* @param parent Parent component for messages
* @param toSave true if we will save to the chosen file, false if we will read from it
* @return String The absolute pah of the filename. Null if cancelled
*/
static public String chooseFilename(JFileChooser chooser, Component parent, boolean toSave) {
String fileName = null;
int result;
if(toSave) {
result = chooser.showSaveDialog(parent);
} else {
result = chooser.showOpenDialog(parent);
}
if(result==JFileChooser.APPROVE_OPTION) {
OSPRuntime.chooserDir = chooser.getCurrentDirectory().toString();
File file = chooser.getSelectedFile();
// check to see if file exists
if(toSave) { // saving: check if the file will be overwritten
if(file.exists()) {
int selected = JOptionPane.showConfirmDialog(parent, DisplayRes.getString("DrawingFrame.ReplaceExisting_message")+" "+file.getName() //$NON-NLS-1$ //$NON-NLS-2$
+DisplayRes.getString("DrawingFrame.QuestionMark"), DisplayRes.getString( //$NON-NLS-1$
"DrawingFrame.ReplaceFile_option_title"), //$NON-NLS-1$
JOptionPane.YES_NO_CANCEL_OPTION);
if(selected!=JOptionPane.YES_OPTION) {
return null;
}
}
} else { // Reading: check if thefile actually exists
if(!file.exists()) {
JOptionPane.showMessageDialog(parent, DisplayRes.getString("GUIUtils.FileDoesntExist")+" "+file.getName(), //$NON-NLS-1$ //$NON-NLS-2$
DisplayRes.getString("GUIUtils.FileChooserError"), //$NON-NLS-1$
JOptionPane.ERROR_MESSAGE);
return null;
}
}
fileName = file.getAbsolutePath();
if((fileName==null)||fileName.trim().equals("")) { //$NON-NLS-1$
return null;
}
}
return fileName;
}
private void otherButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_otherButtonActionPerformed
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
otherField.setText(fc.getSelectedFile().getAbsolutePath());
otherRadio.setSelected(true);
defaultZipField();
}
}
private void ButtonSelFileActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_ButtonSelFileActionPerformed
{//GEN-HEADEREND:event_ButtonSelFileActionPerformed
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = fc.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
SelFolder = fc.getSelectedFile();
FilePathTextField.setText(SelFolder.getAbsolutePath());
}
}
private void browseFiles() {
final JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setDialogType(JFileChooser.OPEN_DIALOG);
chooser.setDialogTitle(Bundle.DirectorySelectorCombo_DialogCaption());
chooser.setSelectedFile(new File(lastSelectedPath));
chooser.setFileFilter(new FileFilter() {
public boolean accept(File f) {
if (f.isDirectory())
return true;
String path = f.getAbsolutePath();
String ext = path.substring(path.lastIndexOf('.') + 1); // NOI18N
return supportedExtensions.contains(ext);
}
public String getDescription() {
return Bundle.DirectorySelectorCombo_DialogFilter();
}
});
final int returnVal = chooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
final File dir = chooser.getSelectedFile();
ComboListElement newPath = addPath(dir.getAbsolutePath());
fileMRU.setSelectedItem(newPath);
newPath.select();
} else if (returnVal == JFileChooser.CANCEL_OPTION) {
fileMRU.setSelectedItem(lastSelectedObject);
}
}
public static File saveDialog(String fileName) {
JFileChooser fileChooser = createFileChooser();
fileChooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
fileChooser.setSelectedFile(new File(fileName));
int option = fileChooser.showSaveDialog(null);
if (option == JFileChooser.APPROVE_OPTION) {
return fileChooser.getSelectedFile();
}
return null;
}
/**
* Lets user choose a layout in a file chooser popup and then loads/applies it
*/
private void loadLayoutUserDefined() {
JFileChooser jfc = new JFileChooser();
jfc.setCurrentDirectory(getLayoutDirectory());
if (jfc.showOpenDialog(getContentPane()) != JFileChooser.APPROVE_OPTION) return; // cancel pressed
loadLayout(jfc.getSelectedFile(),false);
}
public static void main(final String[] args) throws Exception {
final JFileChooser opener = new JFileChooser(System.getProperty(
"user.home"));
final int result = opener.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
readImg(opener.getSelectedFile());
}
System.exit(0);
}
/**
* Install the game.
* @param askDir ask for the installation directory?
*/
void doInstall(boolean askDir) {
if (askDir) {
JFileChooser fc = new JFileChooser(installDir);
fc.setMultiSelectionEnabled(false);
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (fc.showSaveDialog(this) != JFileChooser.APPROVE_OPTION) {
return;
}
installDir = fc.getSelectedFile();
} else {
installDir = currentDir;
}
final LModule g = updates.getModule(GAME);
showHideProgress(true);
currentAction.setText(label("Checking existing game files..."));
currentFileProgress.setText("0%");
totalFileProgress.setText("0%");
fileProgress.setValue(0);
totalProgress.setValue(0);
install.setVisible(false);
update.setVisible(false);
verifyBtn.setVisible(false);
cancel.setVisible(true);
totalProgress.setVisible(true);
totalFileProgress.setVisible(true);
totalFileProgressLabel.setVisible(true);
worker = new SwingWorker<List<LFile>, Void>() {
@Override
protected List<LFile> doInBackground() throws Exception {
return collectDownloads(g);
}
@Override
protected void done() {
showHideProgress(false);
worker = null;
cancel.setVisible(false);
try {
doDownload(get());
} catch (CancellationException ex) {
} catch (ExecutionException | InterruptedException ex) {
Exceptions.add(ex);
errorMessage(format("Error while checking files: %s", ex));
}
}
};
worker.execute();
}