下面列出了javax.swing.JFileChooser#addActionListener ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static void createAndShowGUI() {
file = getTempFile();
final JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 300);
fileChooser = new JFileChooser(file.getParentFile());
fileChooser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
defaultKeyPressed = true;
frame.dispose();
}
});
frame.getContentPane().add(BorderLayout.CENTER, fileChooser);
frame.setSize(fileChooser.getPreferredSize());
frame.setVisible(true);
}
private static void createAndShowGUI() {
file = getTempFile();
final JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 300);
fileChooser = new JFileChooser(file.getParentFile());
fileChooser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
defaultKeyPressed = true;
frame.dispose();
}
});
frame.getContentPane().add(BorderLayout.CENTER, fileChooser);
frame.setSize(fileChooser.getPreferredSize());
frame.setVisible(true);
}
private static void createAndShowGUI() {
file = getTempFile();
final JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 300);
fileChooser = new JFileChooser(file.getParentFile());
fileChooser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
defaultKeyPressed = true;
frame.dispose();
}
});
frame.getContentPane().add(BorderLayout.CENTER, fileChooser);
frame.setSize(fileChooser.getPreferredSize());
frame.setVisible(true);
}
private static void createAndShowGUI() {
file = getTempFile();
final JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 300);
fileChooser = new JFileChooser(file.getParentFile());
fileChooser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
defaultKeyPressed = true;
frame.dispose();
}
});
frame.getContentPane().add(BorderLayout.CENTER, fileChooser);
frame.setSize(fileChooser.getPreferredSize());
frame.setVisible(true);
}
private static void createAndShowGUI() {
file = getTempFile();
final JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 300);
fileChooser = new JFileChooser(file.getParentFile());
fileChooser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
defaultKeyPressed = true;
frame.dispose();
}
});
frame.getContentPane().add(BorderLayout.CENTER, fileChooser);
frame.setSize(fileChooser.getPreferredSize());
frame.setVisible(true);
}
/**
* Creates a dialog to choose a file to load.
*
* @param freeColClient The {@code FreeColClient} for the game.
* @param frame The owner frame.
* @param directory The directory to display when choosing the file.
* @param fileFilters The available file filters in the dialog.
* @param defaultName Name of the default save game file.
*/
public SaveDialog(FreeColClient freeColClient, JFrame frame,
File directory, FileFilter[] fileFilters, String defaultName) {
super(freeColClient, frame);
final JFileChooser fileChooser = new JFileChooser(directory);
if (fileFilters.length > 0) {
for (FileFilter fileFilter : fileFilters) {
fileChooser.addChoosableFileFilter(fileFilter);
}
fileChooser.setFileFilter(fileFilters[0]);
fileChooser.setAcceptAllFileFilterUsed(false);
}
fileChooser.setDialogType(JFileChooser.SAVE_DIALOG);
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setFileHidingEnabled(false);
fileChooser.setSelectedFile(new File(defaultName));
fileChooser.addActionListener((ActionEvent ae) ->
setValue((JFileChooser.APPROVE_SELECTION
.equals(ae.getActionCommand()))
? fileChooser.getSelectedFile() : cancelFile));
List<ChoiceItem<File>> c = choices();
initializeDialog(frame, DialogType.QUESTION, true, fileChooser, null, c);
}
private static void createAndShowGUI() {
file = getTempFile();
final JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 300);
fileChooser = new JFileChooser(file.getParentFile());
fileChooser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
defaultKeyPressed = true;
frame.dispose();
}
});
frame.getContentPane().add(BorderLayout.CENTER, fileChooser);
frame.setSize(fileChooser.getPreferredSize());
frame.setVisible(true);
}
private static void createAndShowGUI() {
file = getTempFile();
final JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 300);
fileChooser = new JFileChooser(file.getParentFile());
fileChooser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
defaultKeyPressed = true;
frame.dispose();
}
});
frame.getContentPane().add(BorderLayout.CENTER, fileChooser);
frame.setSize(fileChooser.getPreferredSize());
frame.setVisible(true);
}
private static void createAndShowGUI() {
file = getTempFile();
final JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 300);
fileChooser = new JFileChooser(file.getParentFile());
fileChooser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
defaultKeyPressed = true;
frame.dispose();
}
});
frame.getContentPane().add(BorderLayout.CENTER, fileChooser);
frame.setSize(fileChooser.getPreferredSize());
frame.setVisible(true);
}
private static void createAndShowGUI() {
file = getTempFile();
final JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 300);
fileChooser = new JFileChooser(file.getParentFile());
fileChooser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
defaultKeyPressed = true;
frame.dispose();
}
});
frame.getContentPane().add(BorderLayout.CENTER, fileChooser);
frame.setSize(fileChooser.getPreferredSize());
frame.setVisible(true);
}
private static void createAndShowGUI() {
file = getTempFile();
final JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 300);
fileChooser = new JFileChooser(file.getParentFile());
fileChooser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
defaultKeyPressed = true;
frame.dispose();
}
});
frame.getContentPane().add(BorderLayout.CENTER, fileChooser);
frame.setSize(fileChooser.getPreferredSize());
frame.setVisible(true);
}
private static void createAndShowGUI() {
file = getTempFile();
final JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 300);
fileChooser = new JFileChooser(file.getParentFile());
fileChooser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
defaultKeyPressed = true;
frame.dispose();
}
});
frame.getContentPane().add(BorderLayout.CENTER, fileChooser);
frame.setSize(fileChooser.getPreferredSize());
frame.setVisible(true);
}
private static void createAndShowGUI() {
file = getTempFile();
final JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 300);
fileChooser = new JFileChooser(file.getParentFile());
fileChooser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
defaultKeyPressed = true;
frame.dispose();
}
});
frame.getContentPane().add(BorderLayout.CENTER, fileChooser);
frame.setSize(fileChooser.getPreferredSize());
frame.setVisible(true);
}
private static void createAndShowGUI() {
file = getTempFile();
final JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 300);
fileChooser = new JFileChooser(file.getParentFile());
fileChooser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
defaultKeyPressed = true;
frame.dispose();
}
});
frame.getContentPane().add(BorderLayout.CENTER, fileChooser);
frame.setSize(fileChooser.getPreferredSize());
frame.setVisible(true);
}
public ImportSettingsAccessory(JFileChooser fileChooser) {
this.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.NORTHWEST;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1;
c.gridx = 0;
c.gridy = 0;
c.insets.set(0, 5, 0, 0);
JLabel folderNameLabel = new JLabel("Insert in folder");
this.add(folderNameLabel, c);
c.gridy++;
Set<String> folderNameList = ImportUtils.getFolderNames(true);
folderNameCombo = new JComboBox(folderNameList.toArray());
folderNameCombo.setEditable(true);
this.add(folderNameCombo, c);
c.gridy++;
c.insets.set(10, 5, 0, 0);
c.weighty = 1;
skipCheckBox = new JCheckBox(VisualConstants.getImportDirectlyText());
if (!Session.getSession().getApplication().isStandalone()) {
skipCheckBox.setSelected(true);
} else {
skipCheckBox.setSelected(false);
}
this.add(skipCheckBox, c);
// listen to file chooser for events that are related to this accessory
fileChooser.addActionListener(this);
}
/**
* Creates a dialog to choose a file to load.
*
* @param freeColClient The {@code FreeColClient} for the game.
* @param frame The owner frame.
* @param directory The directory to display when choosing the file.
* @param fileFilters The available file filters in the dialog.
*/
public LoadDialog(FreeColClient freeColClient, JFrame frame,
File directory, FileFilter[] fileFilters) {
super(freeColClient, frame);
final JFileChooser fileChooser = new JFileChooser(directory);
if (fileFilters.length > 0) {
for (FileFilter fileFilter : fileFilters) {
fileChooser.addChoosableFileFilter(fileFilter);
}
fileChooser.setFileFilter(fileFilters[0]);
fileChooser.setAcceptAllFileFilterUsed(false);
}
fileChooser.setControlButtonsAreShown(true);
fileChooser.setApproveButtonText(Messages.message("ok"));
//fileChooser.setCancelButtonText(Messages.message("cancel"));
fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setFileHidingEnabled(false);
fileChooser.addActionListener((ActionEvent ae) -> {
final String cmd = ae.getActionCommand();
File value = (JFileChooser.APPROVE_SELECTION.equals(cmd))
? ((JFileChooser)ae.getSource()).getSelectedFile()
: cancelFile;
setValue(value);
});
List<ChoiceItem<File>> c = choices();
initializeDialog(frame, DialogType.QUESTION, true, fileChooser, null, c);
}
public void loadFromFile() {
final JFileChooser chooser = new AccessibleJFileChooser(NbBundle.getMessage(SvnProperties.class, "ACSD_Properties"));
chooser.setDialogTitle(NbBundle.getMessage(SvnProperties.class, "CTL_Load_Value_Title"));
chooser.setMultiSelectionEnabled(false);
javax.swing.filechooser.FileFilter[] fileFilters = chooser.getChoosableFileFilters();
for (int i = 0; i < fileFilters.length; i++) {
javax.swing.filechooser.FileFilter fileFilter = fileFilters[i];
chooser.removeChoosableFileFilter(fileFilter);
}
chooser.setCurrentDirectory(roots[0].getParentFile()); // NOI18N
chooser.addChoosableFileFilter(new javax.swing.filechooser.FileFilter() {
@Override
public boolean accept(File f) {
return f.exists();
}
@Override
public String getDescription() {
return "";
}
});
chooser.setDialogType(JFileChooser.OPEN_DIALOG);
chooser.setApproveButtonMnemonic(NbBundle.getMessage(SvnProperties.class, "MNE_LoadValue").charAt(0));
chooser.setApproveButtonText(NbBundle.getMessage(SvnProperties.class, "CTL_LoadValue"));
DialogDescriptor dd = new DialogDescriptor(chooser, NbBundle.getMessage(SvnProperties.class, "CTL_Load_Value_Title"));
dd.setOptions(new Object[0]);
final Dialog dialog = DialogDisplayer.getDefault().createDialog(dd);
chooser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String state = e.getActionCommand();
if (state.equals(JFileChooser.APPROVE_SELECTION)) {
File source = chooser.getSelectedFile();
if (Utils.isFileContentText(source)) {
if (source.canRead()) {
StringWriter sw = new StringWriter();
try {
Utils.copyStreamsCloseAll(sw, new FileReader(source));
panel.txtAreaValue.setText(sw.toString());
} catch (IOException ex) {
Subversion.LOG.log(Level.SEVERE, null, ex);
}
}
} else {
handleBinaryFile(source);
}
}
dialog.dispose();
}
});
dialog.setVisible(true);
}
private JFileChooser createFileChooser(File curentDir) {
final JFileChooser chooser = new AccessibleJFileChooser(NbBundle.getMessage(ExportDiffSupport.class, "ACSD_Export"));
chooser.setDialogTitle(NbBundle.getMessage(ExportDiffSupport.class, "CTL_Export_Title"));
chooser.setMultiSelectionEnabled(false);
javax.swing.filechooser.FileFilter[] old = chooser.getChoosableFileFilters();
for (int i = 0; i < old.length; i++) {
javax.swing.filechooser.FileFilter fileFilter = old[i];
chooser.removeChoosableFileFilter(fileFilter);
}
chooser.setCurrentDirectory(curentDir); // NOI18N
chooser.addChoosableFileFilter(getFileFilter());
chooser.setDialogType(JFileChooser.SAVE_DIALOG);
chooser.setApproveButtonMnemonic(NbBundle.getMessage(ExportDiffSupport.class, "MNE_Export_ExportAction").charAt(0));
chooser.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String state = e.getActionCommand();
if (state.equals(JFileChooser.APPROVE_SELECTION)) {
File destination = chooser.getSelectedFile();
destination = getTargetFile(destination);
if (destination.exists()) {
NotifyDescriptor nd = new NotifyDescriptor.Confirmation(NbBundle.getMessage(ExportDiffSupport.class, "BK3005", destination.getAbsolutePath()));
nd.setOptionType(NotifyDescriptor.YES_NO_OPTION);
DialogDisplayer.getDefault().notify(nd);
if (nd.getValue().equals(NotifyDescriptor.OK_OPTION) == false) {
return;
}
}
preferences.put("ExportDiff.saveFolder", destination.getParent()); // NOI18N
panel.setOutputFileText(destination.getAbsolutePath());
} else {
dd.setValue(null);
}
if(dialog != null) {
dialog.dispose();
}
}
});
return chooser;
}
private File getPatchFor(FileObject fo) {
JFileChooser chooser = new JFileChooser();
String patchDirPath = DiffModuleConfig.getDefault().getPreferences().get(PREF_RECENT_PATCH_PATH, System.getProperty("user.home"));
File patchDir = new File(patchDirPath);
while (!patchDir.isDirectory()) {
patchDir = patchDir.getParentFile();
if (patchDir == null) {
patchDir = new File(System.getProperty("user.home"));
break;
}
}
FileUtil.preventFileChooserSymlinkTraversal(chooser, patchDir);
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
String title = NbBundle.getMessage(PatchAction.class,
(fo.isData()) ? "TITLE_SelectPatchForFile"
: "TITLE_SelectPatchForFolder", fo.getNameExt());
chooser.setDialogTitle(title);
// setup filters, default one filters patch files
FileFilter patchFilter = new javax.swing.filechooser.FileFilter() {
@Override
public boolean accept(File f) {
return f.getName().endsWith("diff") || f.getName().endsWith("patch") || f.isDirectory(); // NOI18N
}
@Override
public String getDescription() {
return NbBundle.getMessage(PatchAction.class, "CTL_PatchDialog_FileFilter");
}
};
chooser.addChoosableFileFilter(patchFilter);
chooser.setFileFilter(patchFilter);
chooser.setApproveButtonText(NbBundle.getMessage(PatchAction.class, "BTN_Patch"));
chooser.setApproveButtonMnemonic(NbBundle.getMessage(PatchAction.class, "BTN_Patch_mnc").charAt(0));
chooser.setApproveButtonToolTipText(NbBundle.getMessage(PatchAction.class, "BTN_Patch_tooltip"));
HelpCtx ctx = new HelpCtx(PatchAction.class.getName());
DialogDescriptor descriptor = new DialogDescriptor( chooser, title, true, new Object[0], null, 0, ctx, null );
final Dialog dialog = DialogDisplayer.getDefault().createDialog( descriptor );
dialog.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(PatchAction.class, "ACSD_PatchDialog"));
ChooserListener listener = new PatchAction.ChooserListener(dialog,chooser);
chooser.addActionListener(listener);
dialog.setVisible(true);
File selectedFile = listener.getFile();
if (selectedFile != null) {
DiffModuleConfig.getDefault().getPreferences().put(PREF_RECENT_PATCH_PATH, selectedFile.getParentFile().getAbsolutePath());
}
return selectedFile;
}
@Override
protected void performContextAction(Node[] nodes) {
VCSContext ctx = HgUtils.getCurrentContext(nodes);
final File roots[] = HgUtils.getActionRoots(ctx);
if (roots == null || roots.length == 0) {
return;
}
final File root = Mercurial.getInstance().getRepositoryRoot(roots[0]);
final JFileChooser fileChooser = new AccessibleJFileChooser(Bundle.ACSD_ApplyDiffPatchBrowseFolder(), null);
fileChooser.setDialogTitle(Bundle.ApplyDiffPatchBrowse_title());
fileChooser.setMultiSelectionEnabled(false);
fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
fileChooser.setApproveButtonMnemonic(Bundle.ApplyDiffPatch_Apply().charAt(0));
fileChooser.setApproveButtonText(Bundle.ApplyDiffPatch_Apply());
fileChooser.setCurrentDirectory(new File(HgModuleConfig.getDefault().getImportFolder()));
// setup filters, default one filters patch files
FileFilter patchFilter = new javax.swing.filechooser.FileFilter() {
@Override
public boolean accept(File f) {
return f.getName().endsWith("diff") || f.getName().endsWith("patch") || f.isDirectory(); // NOI18N
}
@Override
public String getDescription() {
return Bundle.CTL_PatchDialog_FileFilter();
}
};
fileChooser.addChoosableFileFilter(patchFilter);
fileChooser.setFileFilter(patchFilter);
DialogDescriptor dd = new DialogDescriptor(fileChooser, Bundle.ApplyDiffPatchBrowse_title());
dd.setOptions(new Object[0]);
final Dialog dialog = DialogDisplayer.getDefault().createDialog(dd);
fileChooser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String state = e.getActionCommand();
if (state.equals(JFileChooser.APPROVE_SELECTION)) {
final File patchFile = fileChooser.getSelectedFile();
final RequestProcessor rp = Mercurial.getInstance().getRequestProcessor(root);
new HgProgressSupport() {
@Override
protected void perform () {
if (isNetBeansPatch(patchFile)) {
PatchAction.performPatch(patchFile, roots[0]);
} else {
new ImportDiffAction.ImportDiffProgressSupport(root, patchFile, false, ImportDiffAction.ImportDiffProgressSupport.Kind.PATCH)
.start(rp, root, Bundle.MSG_ApplyDiffPatch_importingPatch());
}
}
}.start(rp, root, Bundle.MSG_ApplyDiffPatch_checkingFile());
}
dialog.dispose();
}
});
dialog.setVisible(true);
}