下面列出了javax.swing.JFileChooser#setCurrentDirectory ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButtonActionPerformed
String command = evt.getActionCommand();
if ("BROWSE".equals(command)) {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(null);
chooser.setDialogTitle("Select Project Location");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
String path = this.projectLocationTextField.getText();
if (path.length() > 0) {
File f = new File(path);
if (f.exists()) {
chooser.setSelectedFile(f);
}
}
if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) {
File projectDir = chooser.getSelectedFile();
projectLocationTextField.setText(FileUtil.normalizeFile(projectDir).getAbsolutePath());
}
panel.fireChangeEvent();
}
}
/**
* Action performed.
*
* @param event the event
* @see java.awt.event.ActionListener#actionPerformed(ActionEvent)
*/
@Override
public void actionPerformed(ActionEvent event) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Load color preferences file");
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
if (this.main.getColorSettingsDir() != null) {
fileChooser.setCurrentDirectory(this.main.getColorSettingsDir());
}
int rc = fileChooser.showOpenDialog(this.main);
if (rc == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
if (file.exists() && file.isFile()) {
this.main.setColorSettingsDir(file.getParentFile());
this.main.setColorSettingFile(file);
try {
this.main.loadColorPreferences(this.main.getColorSettingFile());
} catch (IOException e) {
this.main.handleException(e);
}
}
}
}
/**
* Opens a file chooser and gives the user an opportunity to save the chart
* in PNG format.
*
* @throws IOException if there is an I/O error.
*/
public void doSaveAs() throws IOException {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(this.defaultDirectoryForSaveAs);
FileNameExtensionFilter filter = new FileNameExtensionFilter(
localizationResources.getString("PNG_Image_Files"), "png");
fileChooser.addChoosableFileFilter(filter);
fileChooser.setFileFilter(filter);
int option = fileChooser.showSaveDialog(this);
if (option == JFileChooser.APPROVE_OPTION) {
String filename = fileChooser.getSelectedFile().getPath();
if (isEnforceFileExtensions()) {
if (!filename.endsWith(".png")) {
filename = filename + ".png";
}
}
ChartUtilities.saveChartAsPNG(new File(filename), this.chart,
getWidth(), getHeight());
}
}
private void loadFile() {
JFileChooser fileChooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("HTML Files (*.htm, *.html)", "htm", "html");
fileChooser.addChoosableFileFilter(filter);
fileChooser.addChoosableFileFilter(fileChooser.getAcceptAllFileFilter());
fileChooser.setFileFilter(filter);
if (internalBalloon.getBalloonContentPath().isSetLastUsedMode()) {
fileChooser.setCurrentDirectory(new File(internalBalloon.getBalloonContentPath().getLastUsedPath()));
} else {
fileChooser.setCurrentDirectory(new File(internalBalloon.getBalloonContentPath().getStandardPath()));
}
int result = fileChooser.showSaveDialog(getTopLevelAncestor());
if (result == JFileChooser.CANCEL_OPTION) return;
try {
String exportString = fileChooser.getSelectedFile().toString();
browseText.setText(exportString);
internalBalloon.getBalloonContentPath().setLastUsedPath(fileChooser.getCurrentDirectory().getAbsolutePath());
internalBalloon.getBalloonContentPath().setPathMode(PathMode.LASTUSED);
}
catch (Exception e) {
//
}
}
/**
* opens Language file
*/
public void open() {
String curFileName = core.getCurFileName();
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("Open Language File");
FileNameExtensionFilter filter = new FileNameExtensionFilter("PolyGlot Languages", "pgd");
chooser.setFileFilter(filter);
String fileName;
if (curFileName.isEmpty()) {
chooser.setCurrentDirectory(core.getWorkingDirectory());
} else {
chooser.setCurrentDirectory(IOHandler.getDirectoryFromPath(curFileName));
}
if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
// only open if save/cancel test is passed
if (!saveOrCancelTest()) {
return;
}
fileName = chooser.getSelectedFile().getAbsolutePath();
openFileFromPath(fileName);
}
genTitle();
}
private void loadFile() {
JFileChooser fileChooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("HTML Files (*.htm, *.html)", "htm", "html");
fileChooser.addChoosableFileFilter(filter);
fileChooser.addChoosableFileFilter(fileChooser.getAcceptAllFileFilter());
fileChooser.setFileFilter(filter);
if (internalBalloon.getBalloonContentPath().isSetLastUsedMode()) {
fileChooser.setCurrentDirectory(new File(internalBalloon.getBalloonContentPath().getLastUsedPath()));
} else {
fileChooser.setCurrentDirectory(new File(internalBalloon.getBalloonContentPath().getStandardPath()));
}
int result = fileChooser.showSaveDialog(getTopLevelAncestor());
if (result == JFileChooser.CANCEL_OPTION) return;
try {
String exportString = fileChooser.getSelectedFile().toString();
browseText.setText(exportString);
internalBalloon.getBalloonContentPath().setLastUsedPath(fileChooser.getCurrentDirectory().getAbsolutePath());
internalBalloon.getBalloonContentPath().setPathMode(PathMode.LASTUSED);
}
catch (Exception e) {
//
}
}
private void jButton_DataFolderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_DataFolderActionPerformed
// TODO add your handling code here:
JFileChooser aDlg = new JFileChooser();
String path = System.getProperty("user.dir");
File pathDir = new File(path);
if (pathDir.isDirectory()) {
aDlg.setCurrentDirectory(pathDir);
}
aDlg.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (JFileChooser.APPROVE_OPTION == aDlg.showOpenDialog(this)) {
File aFile = aDlg.getSelectedFile();
System.setProperty("user.dir", aFile.getAbsolutePath());
this.setDataFolder(aFile);
}
}
private void onMM5IMDataClick(ActionEvent e) {
this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
String path = this.getStartupPath();
File pathDir = new File(path);
JFileChooser aDlg = new JFileChooser();
aDlg.setCurrentDirectory(pathDir);
if (JFileChooser.APPROVE_OPTION == aDlg.showOpenDialog(this)) {
File file = aDlg.getSelectedFile();
//this._parent.setCurrentDataFolder(file.getParent());
System.setProperty("user.dir", file.getParent());
MeteoDataInfo aDataInfo = new MeteoDataInfo();
aDataInfo.openMM5IMData(file.getAbsolutePath());
addMeteoData(aDataInfo);
}
this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
protected void onPostConstruct(JFileChooser ofd) {
if (optionalRecentlyUsedFolder != null) {
String startFolder = optionalRecentlyUsedFolder.getValue();
if (startFolder != null) {
ofd.setCurrentDirectory(new File(startFolder));
ofd.setSelectedFile(new File(startFolder));
}
}
}
public static File[] showOpenFolderDialog( final Component parent, final String title, final boolean multiselection,
final File initialPath ) {
RunnableWithParameters runnable = new RunnableWithParameters(){
public void run() {
JFileChooser fc = new JFileChooser();
fc.setDialogTitle(title);
fc.setDialogType(JFileChooser.OPEN_DIALOG);
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fc.setMultiSelectionEnabled(multiselection);
fc.setCurrentDirectory(initialPath);
fc.setFileHidingEnabled(false);
int r = fc.showOpenDialog(parent);
if (r != JFileChooser.APPROVE_OPTION) {
this.returnValue = null;
return;
}
if (fc.isMultiSelectionEnabled()) {
File[] selectedFiles = fc.getSelectedFiles();
this.returnValue = selectedFiles;
} else {
File selectedFile = fc.getSelectedFile();
if (selectedFile != null && selectedFile.exists())
PreferencesHandler.setLastPath(selectedFile.getAbsolutePath());
this.returnValue = new File[]{selectedFile};
}
}
};
if (SwingUtilities.isEventDispatchThread()) {
runnable.run();
} else {
try {
SwingUtilities.invokeAndWait(runnable);
} catch (Exception e) {
Logger.INSTANCE.insertError("", "Can't show chooser dialog '" + title + "'.", e);
}
}
return (File[]) runnable.getReturnValue();
}
private void AnalyzeFileBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AnalyzeFileBtnActionPerformed
// Select a file
JFileChooser fc = new JFileChooser();
fc.setCurrentDirectory(new java.io.File("."));
int iRes = fc.showOpenDialog(this);
if (iRes != JFileChooser.APPROVE_OPTION)
return;
checkFile(fc.getSelectedFile().getAbsolutePath(), NEW_CATEGORY, true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == saveToFileButton) {
JFileChooser fileChooser = getFileChooser();
fileChooser.setCurrentDirectory(new File(saveToFileField.getText()).getParentFile());
if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
saveToFileField.setText(fileChooser.getSelectedFile().getAbsolutePath());
}
}
}
/**
* Open a keystore file from disk.
*
* @return True if a keystore is opened, false otherwise
*/
private boolean openKeyStoreFile()
{
// Does the current keystore contain unsaved changes?
if (needSave())
{
// Yes - ask the user if it should be saved
int iWantSave = wantSave();
if ((iWantSave == JOptionPane.YES_OPTION && !saveKeyStore()) || iWantSave == JOptionPane.CANCEL_OPTION)
{
return false;
}
}
// Let the user choose a file to open from
JFileChooser chooser = FileChooserFactory.getKeyStoreFileChooser(null);
File fLastDir = m_lastDir.getLastDir();
if (fLastDir != null)
{
chooser.setCurrentDirectory(fLastDir);
}
chooser.setDialogTitle(RB.getString("FPortecle.OpenKeyStoreFile.Title"));
chooser.setMultiSelectionEnabled(false);
int iRtnValue = chooser.showOpenDialog(this);
if (iRtnValue == JFileChooser.APPROVE_OPTION)
{
File fOpenFile = chooser.getSelectedFile();
// File chosen - open the keystore
if (openKeyStoreFile(fOpenFile, true))
{
return true;
}
}
return false;
}
/**
* Opens a dialog to select a directory for PEAR file installation.
*
* @return Selected installation directory path, or current directory path, if nothing was
* selected.
*/
private String selectDir() {
userPrefs = Preferences.userNodeForPackage(this.getClass());
JFileChooser fileChooser = new JFileChooser();
fileChooser.addChoosableFileFilter(new PEARFilter());
String lastDirName = (installDirTextField.getText().length() > 0) ? installDirTextField
.getText() : userPrefs.get(LAST_DIRECTORY_CHOOSEN_KEY, "./");
String selectedDirName = null;
File directory = (lastDirName.length() > 0) ? new File(lastDirName).getParentFile() : new File(
"./");
fileChooser.setCurrentDirectory(directory);
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int result = fileChooser.showDialog(new JFrame(), "Select");
// User selects the 'Select Button'
if (result == JFileChooser.APPROVE_OPTION) {
selectedDirName = fileChooser.getSelectedFile().getAbsolutePath();
// saving the state of the window
try {
userPrefs.put(LAST_DIRECTORY_CHOOSEN_KEY, selectedDirName);
} catch (NullPointerException ex) {
pearConsole.append("NullPointerException" + ex);
}
installDirTextField.setText(selectedDirName);
installButton.setEnabled(true);
}
// no selection was made
else {
installButton.setEnabled(true);
}
return selectedDirName;
}
@Override
public void actionPerformed(ActionEvent event) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Open XMI CAS file");
if (this.main.getXcasFileOpenDir() != null) {
fileChooser.setCurrentDirectory(this.main.getXcasFileOpenDir());
}
int rc = fileChooser.showOpenDialog(this.main);
if (rc == JFileChooser.APPROVE_OPTION) {
File xmiCasFile = fileChooser.getSelectedFile();
if (xmiCasFile.exists() && xmiCasFile.isFile()) {
this.main.loadXmiFile(xmiCasFile);
}
}
}
/** Select the directory of the peer summaries. */
private void BrowseSummaryDirBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BrowseSummaryDirBtnActionPerformed
// Select a dir
JFileChooser fc = new JFileChooser();
fc.setCurrentDirectory((SummariesRootDirEdt.getText().length() == 0) ?
new java.io.File(".") : new java.io.File(SummariesRootDirEdt.getText()));
fc.setSelectedFile(fc.getCurrentDirectory());
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int iRet = fc.showOpenDialog(this);
if (iRet == JFileChooser.APPROVE_OPTION)
{
SummariesRootDirEdt.setText(fc.getSelectedFile().getAbsolutePath());
}
}
public static JFileChooser registerBrowseButtonListener(final JComboBox comboBox, final JButton button, final boolean chooseFile, final boolean isOpen, final FileFilter fileFilter, final File initialDirectory) {
if(! comboBox.isEditable()) {
throw new IllegalArgumentException("The combo box must be editable.");
}
final JFileChooser fileChooser = new JFileChooser();
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
fileChooser.setCurrentDirectory(comboBox.getSelectedItem() == null || comboBox.getSelectedItem().toString().trim().length() == 0 ?
initialDirectory : new File(comboBox.getSelectedItem().toString()));
fileChooser.setFileSelectionMode(chooseFile ? JFileChooser.FILES_ONLY : JFileChooser.DIRECTORIES_ONLY);
if(fileFilter != null) {
fileChooser.addChoosableFileFilter(fileFilter);
}
int ret = isOpen ? fileChooser.showOpenDialog(comboBox) :
fileChooser.showSaveDialog(comboBox);
if(ret == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
comboBox.getEditor().setItem(file.getAbsolutePath());
}
}
};
button.addActionListener(listener);
return fileChooser;
}
protected void suggestInitialDirectory(JFileChooser ofd) {
try {
String lastChosenDestination = configPairs.find(configPairNameToRemember, null);
String pathname = StringUtils.hasText(lastChosenDestination) ? lastChosenDestination
: SystemUtils.getUserHome().getAbsolutePath();
ofd.setCurrentDirectory(new File(pathname));
} catch (Throwable t) {
log.warn("Failed to set suggested location by key " + configPairNameToRemember, t);
}
}
SelectedFile selectSnapshotTargetFile(final String defaultName, final boolean heapdump) {
String targetName;
FileObject targetDir;
// 1. let the user choose file or directory
JFileChooser chooser = new JFileChooser();
if (exportDir != null) {
chooser.setCurrentDirectory(exportDir);
}
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.setMultiSelectionEnabled(false);
chooser.setDialogTitle(Bundle.ResultsManager_SelectFileOrDirDialogCaption());
chooser.setApproveButtonText(Bundle.ResultsManager_SaveButtonName());
chooser.setAcceptAllFileFilterUsed(false);
chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
public boolean accept(File f) {
return f.isDirectory() || f.getName().endsWith("." + (heapdump ? HEAPDUMP_EXTENSION : SNAPSHOT_EXTENSION)); //NOI18N
}
public String getDescription() {
if (heapdump) {
return Bundle.ResultsManager_ProfilerHeapdumpFileFilter(HEAPDUMP_EXTENSION);
}
return Bundle.ResultsManager_ProfilerSnapshotFileFilter(SNAPSHOT_EXTENSION);
}
});
if (chooser.showSaveDialog(WindowManager.getDefault().getMainWindow()) != JFileChooser.APPROVE_OPTION) {
return null; // cancelled by the user
}
// 2. process both cases and extract file name and extension to use
File file = chooser.getSelectedFile();
String targetExt = heapdump ? HEAPDUMP_EXTENSION : SNAPSHOT_EXTENSION;
if (file.isDirectory()) { // save to selected directory under default name
exportDir = chooser.getCurrentDirectory();
targetDir = FileUtil.toFileObject(FileUtil.normalizeFile(file));
targetName = defaultName;
} else { // save to selected file
exportDir = chooser.getCurrentDirectory();
targetDir = FileUtil.toFileObject(FileUtil.normalizeFile(exportDir));
String fName = file.getName();
// divide the file name into name and extension
int idx = fName.lastIndexOf('.'); // NOI18N
if (idx == -1) { // no extension
targetName = fName;
// extension will be used from source file
} else { // extension exists
if (heapdump || fName.endsWith("." + targetExt)) { // NOI18N
targetName = fName.substring(0, idx);
targetExt = fName.substring(idx + 1);
} else {
targetName = fName;
}
}
}
// 3. return a newly created FileObject
return new SelectedFile(targetDir, targetName, targetExt);
}
private void savejButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_savejButtonActionPerformed
if (this.editDataPanel.getData().getAttributes().size() == 0) {
JOptionPane.showMessageDialog(this, "Please, insert at least one variable", "Error", 2);
return;
}
JFileChooser chooser = new JFileChooser();
KeelFileFilter fileFilter = new KeelFileFilter();
fileFilter.addExtension("dat");
fileFilter.setFilterName("KEEL Files (.dat)");
chooser.setFileFilter(fileFilter);
chooser.setCurrentDirectory(Path.getFilePath());
if(!this.loadjTextField.getText().equals(""))
chooser.setSelectedFile(new File(this.loadjTextField.getText()));
int opcion = chooser.showSaveDialog(this);
Path.setFilePath(chooser.getCurrentDirectory());
String cadena, aux;
boolean ok;
if (opcion == JFileChooser.APPROVE_OPTION && this.editDataPanel.getStateAddButton()) {
String nombre = chooser.getSelectedFile().getAbsolutePath();
if (!nombre.toLowerCase().endsWith(".dat") && !nombre.toLowerCase().endsWith(".txt")) {
// Add correct extension
nombre += ".dat";
}
File tmp = new File(nombre);
if (!tmp.exists() || JOptionPane.showConfirmDialog(this,
"File " + nombre + " already exists. Do you want to replace it?", "Confirm",
JOptionPane.YES_NO_OPTION, 3) == JOptionPane.YES_OPTION) {
imprimeCabecera(nombre);
for (int i = 0; i < this.editDataPanel.getTablaDataset().getData().length; i++) {
cadena = "";
ok = false;
for (int j = 0; j < this.editDataPanel.getData().getNVariables(); j++) {
if (!ok) {
if (this.editDataPanel.getTablaDataset().getData()[i][j] == null ||
(this.editDataPanel.getTablaDataset().getData().toString()).equalsIgnoreCase(
"<null>")) {
cadena += "<null>";
} else {
cadena += this.editDataPanel.getTablaDataset().getData()[i][j];
}
ok = true;
} else {
if (this.editDataPanel.getTablaDataset().getData() == null ||
(this.editDataPanel.getTablaDataset().getData().toString()).equalsIgnoreCase(
"<null>")) {
cadena += ", <null>";
} else {
cadena += ", " + this.editDataPanel.getTablaDataset().getData()[i][j];
}
}
}
cadena += "\n";
Files.addToFile(nombre, cadena);
}
JOptionPane.showMessageDialog(this, "DataSet created", "Info",
JOptionPane.INFORMATION_MESSAGE);
// Perform partitions?
if(showPartition){
Object[] options = {"Yes", "No"};
int n = JOptionPane.showOptionDialog(parent,
"Do you want to make partitions for this dataset?",
"Make partitions",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[1]);
if (n == 0) {
if (dataCFFrame != null) {
dataCFFrame.addPartitionTab(tmp);
}
}
showPartition = false;
}
}
}
}