下面列出了javax.swing.JFileChooser#OPEN_DIALOG 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void updateView(JFileChooser chooser) {
if (chooser.getApproveButtonText() != null) {
approveButton.setText(chooser.getApproveButtonText());
approveButton.setMnemonic(chooser.getApproveButtonMnemonic());
} else {
if (JFileChooser.OPEN_DIALOG == chooser.getDialogType()) {
approveButton.setText(openButtonText);
approveButton.setToolTipText(openButtonToolTipText);
approveButton.setMnemonic(openButtonMnemonic);
} else {
approveButton.setText(saveButtonText);
approveButton.setToolTipText(saveButtonToolTipText);
approveButton.setMnemonic(saveButtonMnemonic);
}
}
cancelButton.setText(cancelButtonText);
cancelButton.setMnemonic(cancelButtonMnemonic);
buttonPanel.setVisible(chooser.getControlButtonsAreShown());
}
/**
* Locate a file for input or output. Note that JavaScript will not return on cancel for OPEN_DIALOG.
*
* @param title The title for the dialog
* @param mode OPEN_DIALOG or SAVE_DIALOG
* @param processFile function to use when complete
*/
public static void getFileAsync(Component parent, String title, int mode, Function<File, Void> processFile) {
// BH no references to this method. So changing its signature for asynchonous use
// And it didn't do as advertised - ran System.exit(0) if canceled
// create and display a file dialog
AsyncFileChooser fc = new AsyncFileChooser();
fc.setDialogTitle(title);
Runnable after = new Runnable() {
@Override
public void run() {
processFile.apply(fc.getSelectedFile());
}
};
if (mode == JFileChooser.OPEN_DIALOG) {
fc.showOpenDialog(parent, after, after);
} else {
fc.showSaveDialog(parent, after, after);
}
}
private static String chooseFile(final int type) {
JFileChooser chooser = new JFileChooser();
chooser.setAcceptAllFileFilterUsed(true);
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setMultiSelectionEnabled(false);
chooser.setFileFilter(new FileFilter() {
public boolean accept(File f) {
if (f.isDirectory())
return true;
String n = f.getName();
String extension = null;
int dotIndex = n.lastIndexOf('.');
if (dotIndex != -1 && dotIndex < n.length() - 1)
extension = n.substring(dotIndex + 1);
if (extension == null)
return false;
if (type == JFileChooser.OPEN_DIALOG)
return extension.equalsIgnoreCase("jpg") || extension.equalsIgnoreCase("png")
|| extension.equalsIgnoreCase("jpeg") || extension.equalsIgnoreCase("gif");
else
return extension.equalsIgnoreCase("png");
}
public String getDescription() {
return "Image Files";
}
});
int ret;
if (type == JFileChooser.OPEN_DIALOG)
ret = chooser.showOpenDialog(null);
else
ret = chooser.showSaveDialog(null);
if (ret == JFileChooser.APPROVE_OPTION) {
return chooser.getSelectedFile().getAbsolutePath();
}
return null;
}
private void loadFile(String extension) throws IOException, ClassNotFoundException {
JFileChooser jfChooser = new JFileChooser();
if (jfChooser.showOpenDialog(this) == JFileChooser.OPEN_DIALOG) {
File file = jfChooser.getSelectedFile();
if (file.getName().contains(".json"))
this.user = new UserFileData().loadJson(file.getAbsolutePath());
if (file.getName().contains(".txt"))
this.user = new UserFileData().loadText(file.getAbsolutePath());
if (file.getName().contains(".bin"))
this.user = new UserFileData().loadBinary(file.getAbsolutePath());
this.fillFields();
}
}
private Icon getApproveButtonIcon(JFileChooser fc) {
if (fc.getDialogType() == JFileChooser.OPEN_DIALOG) {
return FILECHOOSER_OPEN_ICON;
}
if (fc.getDialogType() == JFileChooser.SAVE_DIALOG) {
return FILECHOOSER_SAVE_ICON;
}
if (fc.getDialogType() == JFileChooser.CUSTOM_DIALOG) {
return FILECHOOSER_SELECT_ICON;
}
return FILECHOOSER_SELECT_ICON;
}
private void openProductSubsetDialog() {
Product product = null;
String newProductName = null;
if (getDialogType() == OPEN_DIALOG) {
File file = getSelectedFile();
if (file == null) {
// Should not come here...
return;
}
try {
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
final FileFilter fileFilter = getFileFilter();
String formatName = (fileFilter instanceof SnapFileFilter) ? ((SnapFileFilter) fileFilter).getFormatName() : null;
product = ProductIO.readProduct(file, formatName);
if (product == null) {
String msg = "The product could not be read.";
String optionalMsg = file.isDirectory() ? "\nSelection points to a directory." : "";
Dialogs.showError(msg + optionalMsg);
return;
}
newProductName = createNewProductName(product.getName(), numSubsetProducts++);
} catch (IOException e) {
Dialogs.showError("The product could not be read:\n" + e.getMessage());
} finally {
setCursor(Cursor.getDefaultCursor());
}
} else {
product = productToExport;
if (StringUtils.isNotNullAndNotEmpty(getCurrentFilename())) {
newProductName = getCurrentFilename();
} else {
newProductName = createNewProductName(product.getName(), numSubsetProducts++);
}
}
if (product != null) {
boolean approve = openProductSubsetDialog(product, newProductName);
if (approve && getDialogType() == JFileChooser.OPEN_DIALOG) {
approveSelection();
}
}
updateState();
}
private void openAdvancedDialog() {
clearCurrentAdvancedProductOptions();
File inputFile = getSelectedFile();
boolean canceled = false;
Pair<ProductReaderPlugIn, Boolean> foundPlugin = findPlugins(inputFile);
if(foundPlugin != null){
if(foundPlugin.getKey() == null) {
canceled = foundPlugin.getValue();
}else{
plugin = foundPlugin.getKey();
}
}
boolean addUIComponents = true;
MetadataInspector metadataInspector = null;
if (plugin != null) {
metadataInspector = plugin.getMetadataInspector();
}else{
addUIComponents = false;
}
//if the product does not support Advanced option action
if (addUIComponents && metadataInspector == null) {
int confirm = JOptionPane.showConfirmDialog(null, "The reader does not support the advanced options!\nDo you want to open the product normally?", null, JOptionPane.YES_NO_OPTION);
//if the user want to open the product normally the Advanced Options window will not be displayed
if (confirm == JOptionPane.YES_OPTION) {
addUIComponents = false;
approveSelection();
} else {//if the user choose not to open the product normally the Advanced Option window components are removed
addUIComponents = false;
}
}
if (addUIComponents) {
boolean approve = openAdvancedProduct(metadataInspector, inputFile);
if (approve && getDialogType() == JFileChooser.OPEN_DIALOG) {
approveSelection();
}
updateState();
}else if(plugin == null && !canceled){
Dialogs.showError(Bundle.LBL_NoReaderFoundText() + String.format("%nFile '%s' can not be opened.", inputFile));
}
}
/**
* This method creates and shows a file chooser, depending on the operating
* system. In case the os is Windows or Linux, the standard
* Swing-JFileChooser will be opened. In case the os is Mac OS X, the old
* awt-dialog is used, which looks more nativ.<br><br>
* When the user chose a file, it will be returned, else {@code null} will
* be returned.
*
* @param parent the parent-frame of the file chooser
* @param dlgmode<br>
* - in case of Mac OS X: either {@code FileDialog.LOAD} or
* {@code FileDialog.SAVE} - else: {@code JFileChooser.OPEN_DIALOG} or
* {@code JFileChooser.SAVE_DIALOG}
* @param filemode<br>
* - not important for Mac OS X. - else: {@code JFileChooser.FILES_ONLY} or
* the other file-selection-mode-values
* @param initdir the initial directory which can be set when the dialog is
* shown
* @param initfile the initial file which can be selected when the dialog is
* shown
* @param title the dialog's title
* @param acceptedext the accepted file extensions that will be accepted,
* i.e. the files that are selectable
* @param desc the description of which file types the extensions are
* @param settings a reference to the CSettings-class
* @return The chosen file, or {@code null} if dialog was cancelled
*/
public static File chooseFile(java.awt.Frame parent, int dlgmode, int filemode, String initdir, String initfile, String title, final String[] acceptedext, final String desc, Settings settings) {
File curdir = (null == initdir) ? null : new File(initdir);
JFileChooser fc = createFileChooser(title, filemode, curdir, acceptedext, desc);
int option = (JFileChooser.OPEN_DIALOG == dlgmode) ? fc.showOpenDialog(parent) : fc.showSaveDialog(parent);
if (JFileChooser.APPROVE_OPTION == option) {
return fc.getSelectedFile();
}
return null;
}
/**
* This method creates and shows a file chooser, depending on the operating
* system. In case the os is Windows or Linux, the standard
* Swing-JFileChooser will be opened. In case the os is Mac OS X, the old
* awt-dialog is used, which looks more nativ.<br><br>
* When the user chose a file, it will be returned, else {@code null} will
* be returned.
*
* @param parent the parent-dialog of the file chooser
* @param dlgmode<br>
* - in case of Mac OS X: either {@code FileDialog.LOAD} or
* {@code FileDialog.SAVE} - else: {@code JFileChooser.OPEN_DIALOG} or
* {@code JFileChooser.SAVE_DIALOG}
* @param filemode<br>
* - not important for Mac OS X. - else: {@code JFileChooser.FILES_ONLY} or
* the other file-selection-mode-values
* @param initdir the initial directory which can be set when the dialog is
* shown
* @param initfile the initial file which can be selected when the dialog is
* shown
* @param title the dialog's title
* @param acceptedext the accepted file extensions that will be accepted,
* i.e. the files that are selectable
* @param desc the description of which file types the extensions are
* @param settings a reference to the CSettings-class
* @return The chosen file, or {@code null} if dialog was cancelled
*/
public static File chooseFile(java.awt.Dialog parent, int dlgmode, int filemode, String initdir, String initfile, String title, final String[] acceptedext, final String desc, Settings settings) {
File curdir = (null == initdir) ? null : new File(initdir);
JFileChooser fc = createFileChooser(title, filemode, curdir, acceptedext, desc);
int option = (JFileChooser.OPEN_DIALOG == dlgmode) ? fc.showOpenDialog(parent) : fc.showSaveDialog(parent);
if (JFileChooser.APPROVE_OPTION == option) {
return fc.getSelectedFile();
}
return null;
}