public void actionPerformed(ActionEvent e) { if (readOnly) { return; } JFileChooser fc = getFileChooser(); File currentDirectory = fc.getCurrentDirectory(); if (!currentDirectory.exists()) { JOptionPane.showMessageDialog( fc, newFolderParentDoesntExistText, newFolderParentDoesntExistTitleText, JOptionPane.WARNING_MESSAGE); return; } File newFolder; try { newFolder = fc.getFileSystemView().createNewFolder(currentDirectory); if (fc.isMultiSelectionEnabled()) { fc.setSelectedFiles(new File[] {newFolder}); } else { fc.setSelectedFile(newFolder); } } catch (IOException exc) { JOptionPane.showMessageDialog( fc, newFolderErrorText + newFolderErrorSeparator + exc, newFolderErrorText, JOptionPane.ERROR_MESSAGE); return; } fc.rescanCurrentDirectory(); }
public void actionPerformed(ActionEvent e) { String cmd = (e.getActionCommand()); if (cmd.equals(aboutItem.getText())) JOptionPane.showMessageDialog( this, "Simple Image Program for DB2004\nversion 0.1\nThanks to BvS", "About imageLab", JOptionPane.INFORMATION_MESSAGE); else if (cmd.equals(quitItem.getText())) System.exit(0); else if (cmd.equals(openItem.getText())) { int returnVal = chooser.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { try { pic2 = new Picture(chooser.getSelectedFile().getName()); pic1 = new Picture(pic2.width(), pic2.height()); lab.setIcon(pic2.getJLabel().getIcon()); sliderPanel.setVisible(false); pack(); repaint(); } catch (Exception ex) { JOptionPane.showMessageDialog( this, "Could not open " + chooser.getSelectedFile().getName() + "\n" + ex.getMessage(), "Open Error", JOptionPane.INFORMATION_MESSAGE); } } } else if (cmd.equals(saveItem.getText())) { int returnVal = chooser.showSaveDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { try { pic2.save(chooser.getSelectedFile().getName()); } catch (Exception ex) { JOptionPane.showMessageDialog( this, "Could not write " + chooser.getSelectedFile().getName() + "\n" + ex.getMessage(), "Save Error", JOptionPane.INFORMATION_MESSAGE); } } } }
private void saveDataToFile(File file) { try { PrintStream out = new PrintStream(new FileOutputStream(file)); // Print header line out.print("Time"); for (Sequence seq : seqs) { out.print("," + seq.name); } out.println(); // Print data lines if (seqs.size() > 0 && seqs.get(0).size > 0) { for (int i = 0; i < seqs.get(0).size; i++) { double excelTime = toExcelTime(times.time(i)); out.print(String.format(Locale.ENGLISH, "%.6f", excelTime)); for (Sequence seq : seqs) { out.print("," + getFormattedValue(seq.value(i), false)); } out.println(); } } out.close(); JOptionPane.showMessageDialog( this, Resources.format( Messages.FILE_CHOOSER_SAVED_FILE, file.getAbsolutePath(), file.length())); } catch (IOException ex) { String msg = ex.getLocalizedMessage(); String path = file.getAbsolutePath(); if (msg.startsWith(path)) { msg = msg.substring(path.length()).trim(); } JOptionPane.showMessageDialog( this, Resources.format(Messages.FILE_CHOOSER_SAVE_FAILED_MESSAGE, path, msg), Messages.FILE_CHOOSER_SAVE_FAILED_TITLE, JOptionPane.ERROR_MESSAGE); } }
/** Open a file and load the image. */ public void openFile() { JFileChooser chooser = new JFileChooser(); chooser.setCurrentDirectory(new File(".")); String[] extensions = ImageIO.getReaderFileSuffixes(); chooser.setFileFilter(new FileNameExtensionFilter("Image files", extensions)); int r = chooser.showOpenDialog(this); if (r != JFileChooser.APPROVE_OPTION) return; try { Image img = ImageIO.read(chooser.getSelectedFile()); image = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB); image.getGraphics().drawImage(img, 0, 0, null); } catch (IOException e) { JOptionPane.showMessageDialog(this, e); } repaint(); }
@Override public void approveSelection() { File file = getSelectedFile(); if (file != null) { FileFilter filter = getFileFilter(); if (filter != null && filter instanceof FileNameExtensionFilter) { String[] extensions = ((FileNameExtensionFilter) filter).getExtensions(); boolean goodExt = false; for (String ext : extensions) { if (file.getName().toLowerCase().endsWith("." + ext.toLowerCase())) { goodExt = true; break; } } if (!goodExt) { file = new File(file.getParent(), file.getName() + "." + extensions[0]); } } if (file.exists()) { String okStr = Messages.FILE_CHOOSER_FILE_EXISTS_OK_OPTION; String cancelStr = Messages.FILE_CHOOSER_FILE_EXISTS_CANCEL_OPTION; int ret = JOptionPane.showOptionDialog( this, Resources.format(Messages.FILE_CHOOSER_FILE_EXISTS_MESSAGE, file.getName()), Messages.FILE_CHOOSER_FILE_EXISTS_TITLE, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null, new Object[] {okStr, cancelStr}, okStr); if (ret != JOptionPane.OK_OPTION) { return; } } setSelectedFile(file); } super.approveSelection(); }
// saves the destination file private void saveDest() { int returnVal = destFileChooser.showDialog(this, "Save Destination File"); if (returnVal == JFileChooser.APPROVE_OPTION) { if (destFileChooser.getSelectedFile().exists()) { Object[] options = {"Yes", "No", "Cancel"}; int pressedButtonValue = JOptionPane.showOptionDialog( (JFrame) this, "File exists. Replace it ?", "Question", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[2]); if (pressedButtonValue != JOptionPane.YES_OPTION) return; } String fileName = destFileChooser.getSelectedFile().getAbsolutePath(); notifyHackTranslatorListeners(HackTranslatorEvent.SAVE_DEST, fileName); } }