当前位置: 首页>>代码示例>>Java>>正文


Java JFileChooser.getFileFilter方法代码示例

本文整理汇总了Java中javax.swing.JFileChooser.getFileFilter方法的典型用法代码示例。如果您正苦于以下问题:Java JFileChooser.getFileFilter方法的具体用法?Java JFileChooser.getFileFilter怎么用?Java JFileChooser.getFileFilter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.swing.JFileChooser的用法示例。


在下文中一共展示了JFileChooser.getFileFilter方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: selectExportTargetFile

import javax.swing.JFileChooser; //导入方法依赖的package包/类
private SelectedFile selectExportTargetFile(final ExportProvider exportProvider) {
    File targetDir;
    String targetName;
    String defaultName = exportProvider.getViewName();

    // 1. let the user choose file or directory
    final JFileChooser chooser = getFileChooser();
    if (exportDir != null) {
        chooser.setCurrentDirectory(exportDir);
    }
    int result = chooser.showSaveDialog(WindowManager.getDefault().getRegistry().getActivated());
    if (result != JFileChooser.APPROVE_OPTION) {
        return null; // cancelled by the user
    }

    // 2. process both cases and extract file name and extension to use and set exported file type
    File file = chooser.getSelectedFile();
    String targetExt = null;
    FileFilter selectedFileFilter = chooser.getFileFilter();
    if (selectedFileFilter==null  // workaround for #227659
            ||  selectedFileFilter.getDescription().equals(Bundle.BasicExportAction_ExportDialogCSVFilter())) {
        targetExt=FILE_EXTENSION_CSV;
        exportedFileType=MODE_CSV;
    } else if (selectedFileFilter.getDescription().equals(Bundle.BasicExportAction_ExportDialogTXTFilter())) {
        targetExt=FILE_EXTENSION_TXT;
        exportedFileType=MODE_TXT;
    } else if (selectedFileFilter.getDescription().equals(Bundle.BasicExportAction_ExportDialogBINFilter())) {
        targetExt=FILE_EXTENSION_BIN;
        exportedFileType=MODE_BIN;
    }

    if (file.isDirectory()) { // save to selected directory under default name
        exportDir = file;
        targetDir = file;
        targetName = defaultName;
    } else { // save to selected file
        targetDir = fileChooser.getCurrentDirectory();
        String fName = file.getName();

        // divide the file name into name and extension
        if (fName.endsWith("."+targetExt)) {  // NOI18N
            int idx = fName.lastIndexOf('.'); // NOI18N
            targetName = fName.substring(0, idx);
        } else {            // no extension
            targetName=fName;
        }
    }

    // 3. set type of exported file and return a newly created FileObject

    return new BasicExportAction.SelectedFile(targetDir, targetName, targetExt);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:53,代码来源:BasicExportAction.java

示例2: actionPerformed

import javax.swing.JFileChooser; //导入方法依赖的package包/类
/**
 * {@inheritDoc} Displays a file chooser dialog
 * and opens the selected files.
 */
@Override
public void actionPerformed(ActionEvent e) {
    if (running) {
        return;
    }
    try {
        running = true;
        JFileChooser chooser = prepareFileChooser();
        File[] files;
        try {
            if( Boolean.getBoolean("nb.native.filechooser") ) { //NOI18N
                String oldFileDialogProp = System.getProperty("apple.awt.fileDialogForDirectories"); //NOI18N
                System.setProperty("apple.awt.fileDialogForDirectories", "false"); //NOI18N
                FileDialog fileDialog = new FileDialog(WindowManager.getDefault().getMainWindow());
                fileDialog.setMode(FileDialog.LOAD);
                fileDialog.setDirectory(getCurrentDirectory().getAbsolutePath());
                fileDialog.setTitle(chooser.getDialogTitle());
                fileDialog.setVisible(true);
                if( null != oldFileDialogProp ) {
                    System.setProperty("apple.awt.fileDialogForDirectories", oldFileDialogProp); //NOI18N
                } else {
                    System.clearProperty("apple.awt.fileDialogForDirectories"); //NOI18N
                }

                if( fileDialog.getDirectory() != null && fileDialog.getFile() != null ) {
                    String selFile = fileDialog.getFile();
                    File dir = new File( fileDialog.getDirectory() );
                    files = new File[] { new File( dir, selFile ) };
                    currentDirectory = dir;
                } else {
                    throw new UserCancelException();
                }
            } else {
                files = chooseFilesToOpen(chooser);
                currentDirectory = chooser.getCurrentDirectory();
                if (chooser.getFileFilter() != null) { // #227187
                    currentFileFilter =
                            chooser.getFileFilter().getDescription();
                }
            }
        } catch (UserCancelException ex) {
            return;
        }
        for (int i = 0; i < files.length; i++) {
            OpenFile.openFile(files[i], -1);
        }
    } finally {
        running = false;
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:55,代码来源:OpenFileAction.java

示例3: exportAsImage

import javax.swing.JFileChooser; //导入方法依赖的package包/类
/**
 * Export the current graph as image by using a file selection dialog.
 */
private void exportAsImage() {

	String currentFolder = null;
	if (Application.getGlobalInfo() != null) {
		// --- Get the last selected folder of Agent.GUI ---
		currentFolder = Application.getGlobalInfo().getLastSelectedFolderAsString();
	}

	// --- Create instance of JFileChooser -----------------
	JFileChooser jfc = new JFileChooser();
	jfc.setMultiSelectionEnabled(false);
	jfc.setAcceptAllFileFilterUsed(false);

	// --- Add custom icons for file types. ----------------
	jfc.setFileView(new ImageFileView());
	// --- Add the preview pane. ---------------------------
	jfc.setAccessory(new ImagePreview(jfc));

	// --- Set the file filter -----------------------------
	String[] extensionsJPEG = { ImageUtils.jpg, ImageUtils.jpeg };

	ConfigurableFileFilter filterJPG = new ConfigurableFileFilter(extensionsJPEG, "JPEG - Image");
	ConfigurableFileFilter filterPNG = new ConfigurableFileFilter(ImageUtils.png, "PNG - File");
	ConfigurableFileFilter filterGIF = new ConfigurableFileFilter(ImageUtils.gif, "GIF - Image");

	jfc.addChoosableFileFilter(filterGIF);
	jfc.addChoosableFileFilter(filterJPG);
	jfc.addChoosableFileFilter(filterPNG);

	jfc.setFileFilter(filterPNG);

	// --- Maybe set the current directory -----------------
	if (currentFolder != null) {
		jfc.setCurrentDirectory(new File(currentFolder));
	}

	// === Show dialog and wait on user action =============
	int state = jfc.showSaveDialog(this);
	if (state == JFileChooser.APPROVE_OPTION) {

		ConfigurableFileFilter cff = (ConfigurableFileFilter) jfc.getFileFilter();
		String selectedExtension = cff.getFileExtension()[0];
		String mustExtension = "." + selectedExtension;

		File selectedFile = jfc.getSelectedFile();
		if (selectedFile != null) {
			String selectedPath = selectedFile.getAbsolutePath();
			if (selectedPath.endsWith(mustExtension) == false) {
				selectedPath = selectedPath + mustExtension;
			}
			// ---------------------------------------------
			// --- Export current display to image ---------
			// ---------------------------------------------
			this.exportAsImage(this.getVisualizationViewer(), selectedPath, selectedExtension);
			// ---------------------------------------------

			if (Application.getGlobalInfo() != null) {
				Application.getGlobalInfo().setLastSelectedFolder(jfc.getCurrentDirectory());
			}
		}
	} // end APPROVE_OPTION

}
 
开发者ID:EnFlexIT,项目名称:AgentWorkbench,代码行数:67,代码来源:BasicGraphGui.java

示例4: saveAsImage

import javax.swing.JFileChooser; //导入方法依赖的package包/类
private void saveAsImage(){
	
	// --- Determine image size ----------------------------
	Integer imageWidth = Integer.parseInt(this.getTfImageWidth().getText());
	if (imageWidth==0) {
		JOptionPane.showMessageDialog(this, Language.translate("Bitte nur ganze Zahlen größer 0 eingeben!"), Language.translate("Fehlerhafte Eingabe"), JOptionPane.ERROR_MESSAGE);
		this.setImageDefaultValues();
		return;
	}
	Integer imageHeight = Integer.parseInt(this.getTfImageHeight().getText());
	if (imageHeight==0) {
		JOptionPane.showMessageDialog(this, Language.translate("Bitte nur ganze Zahlen größer 0 eingeben!"), Language.translate("Fehlerhafte Eingabe"), JOptionPane.ERROR_MESSAGE);
		this.setImageDefaultValues();
		return;
	}
	
	String currentFolder = null;
	if (Application.getGlobalInfo()!=null) {
		// --- Get the last selected folder of Agent.GUI ---
		currentFolder = Application.getGlobalInfo().getLastSelectedFolderAsString();
	}

	// --- Create instance of JFileChooser -----------------
	JFileChooser jfc = new JFileChooser();
	jfc.setMultiSelectionEnabled(false);
	jfc.setAcceptAllFileFilterUsed(false);

	// --- Add custom icons for file types. ----------------
	jfc.setFileView(new ImageFileView());
	// --- Add the preview pane. ---------------------------
	jfc.setAccessory(new ImagePreview(jfc));

	// --- Set the file filter -----------------------------
	String[] extensionsJPEG = {ImageUtils.jpg, ImageUtils.jpeg};

	ConfigurableFileFilter filterJPG = new ConfigurableFileFilter(extensionsJPEG, "JPEG - Image");
	ConfigurableFileFilter filterPNG = new ConfigurableFileFilter(ImageUtils.png, "PNG - File");
	ConfigurableFileFilter filterGIF = new ConfigurableFileFilter(ImageUtils.gif, "GIF - Image");

	jfc.addChoosableFileFilter(filterGIF);
	jfc.addChoosableFileFilter(filterJPG);
	jfc.addChoosableFileFilter(filterPNG);

	jfc.setFileFilter(filterPNG);

	// --- Maybe set the current directory -----------------
	if (currentFolder != null) {
		jfc.setCurrentDirectory(new File(currentFolder));
	}
	
	// === Show dialog and wait on user action =============
	int state = jfc.showSaveDialog(this);
	
	if (state == JFileChooser.APPROVE_OPTION) {
		ConfigurableFileFilter cff = (ConfigurableFileFilter) jfc.getFileFilter();
		String selectedExtension = cff.getFileExtension()[0];
		String mustExtension = "." + selectedExtension;

		File selectedFile = jfc.getSelectedFile();
		if (selectedFile != null) {
			String selectedPath = selectedFile.getAbsolutePath();
			if (selectedPath.endsWith(mustExtension) == false) {
				selectedPath = selectedPath + mustExtension;
			}
			
			BufferedImage image = this.exportChartAsImage(imageWidth, imageHeight);
			this.writeImageFile(image, selectedPath, selectedExtension);
			
			if (Application.getGlobalInfo() != null) {
				Application.getGlobalInfo().setLastSelectedFolder(jfc.getCurrentDirectory());
			}
		}
	}
}
 
开发者ID:EnFlexIT,项目名称:AgentWorkbench,代码行数:75,代码来源:ChartEditorJPanel.java

示例5: openAEInputFileDialog

import javax.swing.JFileChooser; //导入方法依赖的package包/类
/**
     * Called when user asks to open data file file dialog.
     */
    @Override
    public void openAEInputFileDialog() {
//        try{Thread.currentThread().sleep(200);}catch(InterruptedException e){}
        float oldScale = viewer.chipCanvas.getScale();
        fileChooser = new JFileChooser();
//            new TypeAheadSelector(fileChooser);
        //com.sun.java.plaf.windows.WindowsFileChooserUI;
//            fileChooser.addKeyListener(new KeyAdapter() {
//                public void keyTyped(KeyEvent e){
//                    System.out.println("keycode="+e.getKeyCode());
//                }
//            });
//            System.out.println("fileChooser.getUIClassID()="+fileChooser.getUIClassID());
//            KeyListener[] keyListeners=fileChooser.getKeyListeners();
        ChipDataFilePreview preview = new ChipDataFilePreview(fileChooser, viewer.getChip());
        // from book swing hacks
        new FileDeleter(fileChooser, preview);
        fileChooser.addPropertyChangeListener(preview);
        fileChooser.setAccessory(preview);
        String lastFilePath = AEViewer.prefs.get("AEViewer.lastFile", "");
        // get the last folder
        viewer.lastFile = new File(lastFilePath);
//            fileChooser.setFileFilter(datFileFilter);
        IndexFileFilter indexFileFilter = new IndexFileFilter();
        fileChooser.addChoosableFileFilter(indexFileFilter);
        DATFileFilter datFileFilter = new DATFileFilter();
        fileChooser.addChoosableFileFilter(datFileFilter);
        if (lastFilter == null) {
            fileChooser.setFileFilter(datFileFilter);
        } else {
            fileChooser.setFileFilter(lastFilter);
        }
        fileChooser.setCurrentDirectory(viewer.lastFile);
        // sets the working directory of the chooser
//            boolean wasPaused=isPaused();
        setPaused(true);
        try {
            int retValue = fileChooser.showOpenDialog(viewer);
            if (retValue == JFileChooser.APPROVE_OPTION) {
                lastFilter = fileChooser.getFileFilter();
                try {
                    viewer.lastFile = fileChooser.getSelectedFile();
                    if (viewer.lastFile != null) {
                        viewer.recentFiles.addFile(viewer.lastFile);
                    }
                    startPlayback(viewer.lastFile);
                } catch (IOException fnf) {
                    log.warning(fnf.toString());
                }
            } else {
                preview.showFile(null);
            }
        } catch (GLException e) {
            log.warning(e.toString());
            preview.showFile(null);
        }
        fileChooser = null;
//        viewer.chipCanvas.setScale(oldScale);
        // restore persistent scale so that we don't get tiny size on next startup
        setPaused(false);
    }
 
开发者ID:SensorsINI,项目名称:jaer,代码行数:65,代码来源:AEPlayer.java


注:本文中的javax.swing.JFileChooser.getFileFilter方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。