Java Code Examples for javax.swing.JFileChooser#CANCEL_OPTION

The following examples show how to use javax.swing.JFileChooser#CANCEL_OPTION . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PluginTester.java    From marvinproject with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void saveImage(){
	int result;
	String path=null;
	
	JFileChooser chooser = new JFileChooser();
	chooser.setDialogType(JFileChooser.SAVE_DIALOG);
	result = chooser.showSaveDialog(this);
	
	if(result == JFileChooser.CANCEL_OPTION){
		return;
	}

	try{
		path = chooser.getSelectedFile().getCanonicalPath();
		newImage.update();
		MarvinImageIO.saveImage(imagePanel.getImage(), path);
	}
	catch(Exception e){
		e.printStackTrace();
	}
}
 
Example 2
Source File: TestaArquivo3.java    From dctb-utfpr-2018-1 with Apache License 2.0 6 votes vote down vote up
public void abrirarquivo() {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    int result = fileChooser.showOpenDialog(null);
    if (result == JFileChooser.CANCEL_OPTION) {
        return;
    }
    arquivo = fileChooser.getSelectedFile();
    System.out.println(arquivo);

    if (arquivo == null || arquivo.getName().equals("")) {
        JOptionPane.showMessageDialog(null, "Nome de Arquivo Inválido", "Nome de Arquivo Inválido", JOptionPane.ERROR_MESSAGE);
    } else {
        try {
            entrada = new BufferedReader(new FileReader(arquivo));
        } catch (IOException ioException) {
            JOptionPane.showMessageDialog(null, "Error ao Abrir Arquivo", "Erro", JOptionPane.ERROR_MESSAGE);
        }
    }
}
 
Example 3
Source File: ReliefBalloonPanel.java    From importer-exporter with Apache License 2.0 6 votes vote down vote up
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) {
		//
	}
}
 
Example 4
Source File: CycleView.java    From views-widgets-samples with Apache License 2.0 6 votes vote down vote up
public static void save(ActionEvent e, CycleView graph) {
  int w = graph.getWidth();
  int h = graph.getHeight();
  BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
  graph.paint(img.createGraphics());
  JFileChooser chooser = new JFileChooser(new File(System.getProperty("user.home")));
  int c = chooser.showSaveDialog(graph);
  if (c == JFileChooser.CANCEL_OPTION) {
    System.out.println("cancel");
    return;
  }
  try {
    File f = chooser.getSelectedFile();
    ImageIO.write(img, "png", f);
    System.out.println(f.getAbsolutePath());

    Desktop.getDesktop().open(f.getParentFile());
  } catch (IOException e1) {
    e1.printStackTrace();
  }
}
 
Example 5
Source File: CycleView.java    From android-ConstraintLayoutExamples with Apache License 2.0 6 votes vote down vote up
public static void save(ActionEvent e, CycleView graph) {
  int w = graph.getWidth();
  int h = graph.getHeight();
  BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
  graph.paint(img.createGraphics());
  JFileChooser chooser = new JFileChooser(new File(System.getProperty("user.home")));
  int c = chooser.showSaveDialog(graph);
  if (c == JFileChooser.CANCEL_OPTION) {
    System.out.println("cancel");
    return;
  }
  try {
    File f = chooser.getSelectedFile();
    ImageIO.write(img, "png", f);
    System.out.println(f.getAbsolutePath());

    Desktop.getDesktop().open(f.getParentFile());
  } catch (IOException e1) {
    e1.printStackTrace();
  }
}
 
Example 6
Source File: AnimPlay.java    From open-ig with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Save animation as Wav.
 */
private static void doSaveAsPng() {
	if (current == null) {
		return;
	}
	JFileChooser fc = new JFileChooser(lastSavePath);
	fc.setAcceptAllFileFilterUsed(true);
	fc.setFileFilter(new FileNameExtensionFilter("PNG files", "PNG"));
	if (fc.showSaveDialog(frame) == JFileChooser.CANCEL_OPTION) {
		return;
	}
	final File sel = fc.getSelectedFile();
	lastSavePath = sel.getParentFile();
	
	saveAsPNGWorker(current, sel, true, frame);
}
 
Example 7
Source File: FileBrowserLine.java    From tectonicus with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void actionPerformed(ActionEvent e)
{
	final int result = browseDialog.showDialog(parent, browseLabel);
	if (result == JFileChooser.APPROVE_OPTION)
	{
		file = browseDialog.getSelectedFile();
	}
	else if (result == JFileChooser.CANCEL_OPTION)
	{
		
	}
	else if (result == JFileChooser.ERROR_OPTION)
	{
		
	}
	
	ignoreTextEvents = true;
	
	setText(file.getAbsolutePath());
	
	ignoreTextEvents = false;
	
	onFileChanged();
}
 
Example 8
Source File: TransportationBalloonPanel.java    From importer-exporter with Apache License 2.0 6 votes vote down vote up
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) {
		//
	}
}
 
Example 9
Source File: VegetationBalloonPanel.java    From importer-exporter with Apache License 2.0 6 votes vote down vote up
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) {
		//
	}
}
 
Example 10
Source File: ThreeDBalloonPanel.java    From importer-exporter with Apache License 2.0 6 votes vote down vote up
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) {
		//
	}
}
 
Example 11
Source File: OnClickListener.java    From CEC-Automatic-Annotation with Apache License 2.0 5 votes vote down vote up
public void handleSaveAsFile() {
	// FileDialog fileDialog = new
	// FileDialog(getJFrame(),"保存...",FileDialog.SAVE);
	// fileDialog.setVisible(true);
	// String fileName = fileDialog.getDirectory()+fileDialog.getFile();
	// 设置对话框的风格
	try {
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
	} catch (Exception e1) {
		MyLogger.logger.info("设置对话框风格出错!" + e1.getMessage());
		e1.printStackTrace();
	}
	JFileChooser jFileChooser = new JFileChooser();
	// jFileChooser.setMultiSelectionEnabled(true);//如果要多选的话,设置这句话即可
	// 设置默认的保存文件名称
	// jFileChooser.setSelectedFile(new
	// File(getTitleContent().getText().toString()));
	int result = jFileChooser.showSaveDialog(null);
	switch (result) {
	case JFileChooser.APPROVE_OPTION:
		// 这一种方法是把显示内容中的标题取出来作为文件名,暂不采用
		// filePath = jFileChooser.getCurrentDirectory() + File.separator +
		// getTitleContent().getText() + ".xml";
		// 这一种方法是把用户输入的作为保存的文件名
		filePath = jFileChooser.getCurrentDirectory() + File.separator + jFileChooser.getSelectedFile().getName() + ".xml";
		MyLogger.logger.info("改变路径之后,文件的保存路径=" + filePath);
		MyLogger.logger.info("Approve (Open or Save) was clicked ");
		MyLogger.logger.info("这是绝对路径:" + jFileChooser.getSelectedFile().getAbsolutePath());
		writeToFile(filePath);
		break;
	case JFileChooser.CANCEL_OPTION:
		MyLogger.logger.info("Cancle or the close-dialog icon was clicked");
		break;
	case JFileChooser.ERROR_OPTION:
		MyLogger.logger.error("Error...");
		break;
	}
}
 
Example 12
Source File: ProjectParametersImporter.java    From mzmine2 with GNU General Public License v2.0 5 votes vote down vote up
private File chooseFile() {
  JFileChooser chooser = new JFileChooser();

  FileNameExtensionFilter filter = new FileNameExtensionFilter("TXT & CSV files", "txt", "csv");
  chooser.setDialogTitle("Please select a file containing project parameter values for files.");
  chooser.setFileFilter(filter);
  int returnVal = chooser.showOpenDialog(desktop.getMainWindow());
  if (returnVal == JFileChooser.CANCEL_OPTION) {
    return null;
  }

  return chooser.getSelectedFile();
}
 
Example 13
Source File: ImportLogPanel.java    From importer-exporter with Apache License 2.0 5 votes vote down vote up
private String browseFile(String title, String oldDir) {
	JFileChooser chooser = new JFileChooser();
	chooser.setDialogTitle(title);
	chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
	chooser.setCurrentDirectory(new File(oldDir));

	int result = chooser.showSaveDialog(getTopLevelAncestor());
	if (result == JFileChooser.CANCEL_OPTION) return "";
	String browseString = chooser.getSelectedFile().toString();
	return browseString;
}
 
Example 14
Source File: VisualMospeed.java    From basicv2 with The Unlicense 5 votes vote down vote up
private void loadProgram() {
	JFileChooser fc = new JFileChooser();
	if (lastDir != null) {
		fc.setCurrentDirectory(lastDir);
	}
	int ret = fc.showOpenDialog(frame);
	if (ret == JFileChooser.CANCEL_OPTION || ret == JFileChooser.ERROR_OPTION) {
		compile.setEnabled(code != null && code.length > 0);
		return;
	}
	load(fc.getSelectedFile());
}
 
Example 15
Source File: RobotTree.java    From RobotBuilder with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void load() {
    if (OKToClose()) {
        int result = fileChooser.showOpenDialog(MainFrame.getInstance());
        if (result == JFileChooser.CANCEL_OPTION) {
            return;
        } else if (result == JFileChooser.ERROR_OPTION) {
            return;
        } else if (result == JFileChooser.APPROVE_OPTION) {
            setFilePath(fileChooser.getSelectedFile().getAbsolutePath());
        }
        load(new File(filePath));
    }
}
 
Example 16
Source File: AreaDeTexto.java    From JCEditor with GNU General Public License v2.0 5 votes vote down vote up
/**
* Este método faz com que o usuário escolha um arquivo para então salva-lo.
* Será usado em duas situações, caso o usuário ainda não tenha salvo o arquivo
* (o arquivo é nulo) ou quando o usuário pressionar o botão de salvar como.
*/
public void salvarComo() {
	if (fileChooser().showSaveDialog(this) == JFileChooser.CANCEL_OPTION) {
		return;
	} else {
		setArquivo(fileChooser().getSelectedFile());
		extensao(fileChooser().getSelectedFile());
			if (arquivo.exists()) {
				JOptionPane.showMessageDialog(this, "Sobrescrever?");
			}
		salvar(texto);
		extensao(arquivo);
	}
}
 
Example 17
Source File: XSLTransformationPanel.java    From importer-exporter with Apache License 2.0 5 votes vote down vote up
private void browseStylesheet(StylesheetComponent component) {
    JFileChooser chooser = new JFileChooser();
    chooser.setDialogTitle(Language.I18N.getString("common.pref.xslt.label.file"));

    FileNameExtensionFilter filter = new FileNameExtensionFilter("XSLT Stylesheet (*.xsl)", "xsl");
    chooser.addChoosableFileFilter(filter);
    chooser.addChoosableFileFilter(chooser.getAcceptAllFileFilter());
    chooser.setFileFilter(filter);

    File currentDirectory = null;
    String stylesheet = component.stylesheet.getText().trim();
    if (!stylesheet.isEmpty()) {
        Path path = Paths.get(stylesheet).getParent();
        if (path != null)
            currentDirectory = path.toFile();
    }

    if (currentDirectory == null) {
        if (lastPath != null)
            currentDirectory = lastPath;
        else
            currentDirectory = ClientConstants.IMPEXP_HOME.resolve(ClientConstants.XSLT_TEMPLATES_DIR).toFile();
    }

    chooser.setCurrentDirectory(currentDirectory);

    int result = chooser.showOpenDialog(getTopLevelAncestor());
    if (result != JFileChooser.CANCEL_OPTION) {
        component.stylesheet.setText(chooser.getSelectedFile().getAbsolutePath());
        lastPath = chooser.getCurrentDirectory();
    }
}
 
Example 18
Source File: AIMerger.java    From appinventor-extensions with Apache License 2.0 5 votes vote down vote up
private String getFileToOpen() {
  JFileChooser projectFC = new JFileChooser(browserPath);
  int validPath = projectFC.showOpenDialog(myCP);
  if (validPath == JFileChooser.ERROR_OPTION || validPath == JFileChooser.CANCEL_OPTION) {
    return null;
  } else {
    return projectFC.getSelectedFile().toString();
  }
}
 
Example 19
Source File: CollectionSelectionDialog.java    From gate-core with GNU Lesser General Public License v3.0 4 votes vote down vote up
/** This method is called when the user press the CANCEL button*/
private void doCancel(){
  buttonPressed = JFileChooser.CANCEL_OPTION;
  this.setVisible(false);
}
 
Example 20
Source File: VideoIO.java    From osp with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Displays a file chooser and returns the chosen files.
 *
 * @param type may be "open", "open video", "save", "insert image"
 * @return the files, or null if no files chosen
 */
public static File[] getChooserFiles(String type) {
  JFileChooser chooser = getChooser();
  chooser.setMultiSelectionEnabled(false);
  chooser.setAcceptAllFileFilterUsed(true);
  int result = JFileChooser.CANCEL_OPTION;
  if(type.toLowerCase().equals("open")) { // open any file //$NON-NLS-1$
    chooser.addChoosableFileFilter(videoFileFilter);
    chooser.setFileFilter(chooser.getAcceptAllFileFilter());
    result = chooser.showOpenDialog(null);
  } 
  else if(type.toLowerCase().equals("open video")) { // open video //$NON-NLS-1$
    chooser.addChoosableFileFilter(videoFileFilter);
    result = chooser.showOpenDialog(null);
  } 
  else if(type.toLowerCase().equals("save")) { // save any file //$NON-NLS-1$
  	// note this sets no file filters but does include acceptAll
    // also sets file name to "untitled"
    String filename = MediaRes.getString("VideoIO.FileName.Untitled"); //$NON-NLS-1$
   chooser.setSelectedFile(new File(filename+"."+defaultXMLExt)); //$NON-NLS-1$
    result = chooser.showSaveDialog(null);
  } 
  else if(type.toLowerCase().equals("insert image")) { //$NON-NLS-1$
    chooser.setMultiSelectionEnabled(true);
    chooser.setAcceptAllFileFilterUsed(false);
    chooser.addChoosableFileFilter(imageFileFilter);
    chooser.setSelectedFile(new File("")); //$NON-NLS-1$
    result = chooser.showOpenDialog(null);
    File[] files = chooser.getSelectedFiles();
    chooser.removeChoosableFileFilter(imageFileFilter);
    chooser.setSelectedFile(new File(""));  //$NON-NLS-1$
    if(result==JFileChooser.APPROVE_OPTION) {
      return files;
    }
  }
  if(result==JFileChooser.APPROVE_OPTION) {
  	File file = chooser.getSelectedFile();
    chooser.removeChoosableFileFilter(videoFileFilter);
    chooser.setSelectedFile(new File(""));  //$NON-NLS-1$
    return new File[] {file};
  }
  return null;
}