Example usage for javax.swing JFileChooser JFileChooser

List of usage examples for javax.swing JFileChooser JFileChooser

Introduction

In this page you can find the example usage for javax.swing JFileChooser JFileChooser.

Prototype

public JFileChooser() 

Source Link

Document

Constructs a JFileChooser pointing to the user's default directory.

Usage

From source file:ProgressMonitorInputStreamTest.java

public TextFrame() {
    setTitle("ProgressMonitorInputStreamTest");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    textArea = new JTextArea();
    add(new JScrollPane(textArea));

    chooser = new JFileChooser();
    chooser.setCurrentDirectory(new File("."));

    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);/*from  w w w  .j  ava2  s  .c  o  m*/
    JMenu fileMenu = new JMenu("File");
    menuBar.add(fileMenu);
    openItem = new JMenuItem("Open");
    openItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            try {
                openFile();
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }
    });

    fileMenu.add(openItem);
    exitItem = new JMenuItem("Exit");
    exitItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            System.exit(0);
        }
    });
    fileMenu.add(exitItem);
}

From source file:net.mitnet.tools.pdf.book.publisher.ui.gui.BaseBookPublisherGUI.java

protected void browseFile(JTextField dirField) {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    int returnVal = fileChooser.showOpenDialog(null);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File selectedFile = fileChooser.getSelectedFile();
        String selectedFilePath = selectedFile.getAbsolutePath();
        dirField.setText(selectedFilePath);
    }//  w  w  w . j a v  a 2s  .c om
}

From source file:analysers.ExportValidated.java

@Override
public void analyze(Cell currentCell) {
    JFileChooser fc = new JFileChooser();
    fc.addChoosableFileFilter(new FileNameExtensionFilter("Comma Separated Values", "csv"));
    fc.addChoosableFileFilter(new FileNameExtensionFilter("JSON file", "json"));
    int rv = fc.showSaveDialog(null);
    if (rv == JFileChooser.APPROVE_OPTION) {
        int b = Prefs.getInt(".TrackApp.FilterValidatedDialog.exportMean", 1);
        if (b != 0) {
            IJ.log("Exporting mean intensity values.");
        } else {//from   w w  w .ja va  2  s.  co m
            IJ.log("Exporting sum of intensity values.");
        }

        File ff = fc.getSelectedFile();
        String fn = fc.getSelectedFile().getAbsolutePath();

        if (fc.getFileFilter().getDescription().contains("JSON")) {
            if (!fn.toLowerCase().endsWith(".json")) {
                fn = fn + ".json";
                ff = new File(ff + ".json");
            }
            IJ.log("Writing a JSON file.");
        } else {
            if (!fn.toLowerCase().endsWith(".csv")) {
                fn = fn + ".csv";
                ff = new File(ff + ".csv");
            }
            IJ.log("Writing a CSV file.");
        }
        IJ.log("Writing to " + fn);

        if (fn.endsWith(".csv")) {
            writeCSV(ff);
        } else if (fn.endsWith(".json")) {
            writeJSON(ff);
        } else {
            IJ.log("Unknown output format for file name " + fn);
        }

    }
}

From source file:com.ev.export.AnnualPDFExporter.java

@Override
public String chooseFile() throws IOException {
    JFileChooser jfc = new JFileChooser();
    jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    jfc.showSaveDialog(MainFrame.getInstance());
    File selectedFile = jfc.getSelectedFile();
    if (selectedFile == null) {
        throw new IOException("No file chosen.");
    }// w w  w  .  j  av  a 2  s .co  m
    return selectedFile.getCanonicalPath() + File.separator + year + PDF_SUFFIX;
}

From source file:com.itd.dbmrgdao.TestTime_newscanner.java

@Ignore
@org.junit.Test/*from  w  w w.  j  a v  a 2  s  .co  m*/
public void test() throws ParseException {

    JFileChooser chooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter("Text File", "txt");
    chooser.setFileFilter(filter);
    chooser.showOpenDialog(null);
    File f = chooser.getSelectedFile();

    String fileName = f.getAbsolutePath();
    try {
        FileReader reader = new FileReader(fileName);
        BufferedReader br = new BufferedReader(reader);
        String strLine;

        Boolean result = updateScanFileRoundUp2(br);

        br.close();

    } catch (IOException ex) {

    }

}

From source file:SwingDnDTest.java

public SwingDnDFrame() {
    setTitle("SwingDnDTest");
    JTabbedPane tabbedPane = new JTabbedPane();

    JList list = SampleComponents.list();
    tabbedPane.addTab("List", list);
    JTable table = SampleComponents.table();
    tabbedPane.addTab("Table", table);
    JTree tree = SampleComponents.tree();
    tabbedPane.addTab("Tree", tree);
    JFileChooser fileChooser = new JFileChooser();
    tabbedPane.addTab("File Chooser", fileChooser);
    JColorChooser colorChooser = new JColorChooser();
    tabbedPane.addTab("Color Chooser", colorChooser);

    final JTextArea textArea = new JTextArea(4, 40);
    JScrollPane scrollPane = new JScrollPane(textArea);
    scrollPane.setBorder(new TitledBorder(new EtchedBorder(), "Drag text here"));

    JTextField textField = new JTextField("Drag color here");
    textField.setTransferHandler(new TransferHandler("background"));

    tabbedPane.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            textArea.setText("");
        }/*  ww w . j av  a 2  s.c  o m*/
    });

    tree.setDragEnabled(true);
    table.setDragEnabled(true);
    list.setDragEnabled(true);
    fileChooser.setDragEnabled(true);
    colorChooser.setDragEnabled(true);
    textField.setDragEnabled(true);

    add(tabbedPane, BorderLayout.NORTH);
    add(scrollPane, BorderLayout.CENTER);
    add(textField, BorderLayout.SOUTH);
    pack();
}

From source file:SwingUtil.java

/**
 * Open a JFileChooser dialog for selecting a directory and return the
 * selected directory./*from  ww  w. ja  va2  s.  c om*/
 *
 *
 * @param owner
 * The frame or dialog that controls the invokation of this dialog.
 * @param defaultDir
 * The directory to show when the dialog opens.
 * @param title
 * Tile for the dialog.
 *
 * @return
 * The selected directory as a File. Null if user cancels dialog without
 * a selection.
 *
 */
public static File getDirectoryChoice(Component owner, File defaultDir, String title) {
    //
    // There is apparently a bug in the native Windows FileSystem class that
    // occurs when you use a file chooser and there is a security manager
    // active. An error dialog is displayed indicating there is no disk in
    // Drive A:. To avoid this, the security manager is temporarily set to
    // null and then reset after the file chooser is closed.
    //
    SecurityManager sm = null;
    JFileChooser chooser = null;
    File choice = null;

    sm = System.getSecurityManager();
    System.setSecurityManager(null);
    chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    if ((defaultDir != null) && defaultDir.exists() && defaultDir.isDirectory()) {
        chooser.setCurrentDirectory(defaultDir);
        chooser.setSelectedFile(defaultDir);
    }
    chooser.setDialogTitle(title);
    chooser.setApproveButtonText("OK");
    int v = chooser.showOpenDialog(owner);

    owner.requestFocus();
    switch (v) {
    case JFileChooser.APPROVE_OPTION:
        if (chooser.getSelectedFile() != null) {
            if (chooser.getSelectedFile().exists()) {
                choice = chooser.getSelectedFile();
            } else {
                File parentFile = new File(chooser.getSelectedFile().getParent());

                choice = parentFile;
            }
        }
        break;
    case JFileChooser.CANCEL_OPTION:
    case JFileChooser.ERROR_OPTION:
    }
    chooser.removeAll();
    chooser = null;
    System.setSecurityManager(sm);
    return choice;
}

From source file:gd.gui.GeneticDrawingView.java

@Action
public void chooseImage() throws IOException {
    JFileChooser fc = new JFileChooser();
    fc.setCurrentDirectory(new File("."));
    fc.showOpenDialog(mainPanel);/*w  w  w  .j  a  v  a 2  s . c  o  m*/

    File file = fc.getSelectedFile();
    targetImage = ImageIO.read(file);
    targetImageLabel.setIcon(scaleToImageLabel(targetImage));
    fittestDrawingView.setSize(targetImage.getWidth(), targetImage.getHeight());
}

From source file:kuvalataaja.user_interface.GUI.java

/**
 * Select a directory and pass it to Main.run
 *///from  w w  w. j ava  2  s .co  m
public void runTheProgram() {
    String separatorString = openSettings();
    char separator = separatorString.charAt(0);
    JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    chooser.showDialog(this, "Please select your Music folder");
    File f = new File(chooser.getSelectedFile().getPath());
    textArea.setText("Working...");
    String[] result = kuvalataaja.kuvalataaja.Main.run(f, separator);
    //Shows a list of which album covers were fetched
    showResult(result);
}

From source file:components.FileChooserDemo.java

public FileChooserDemo() {
    super(new BorderLayout());

    //Create the log first, because the action listeners
    //need to refer to it.
    log = new JTextArea(5, 20);
    log.setMargin(new Insets(5, 5, 5, 5));
    log.setEditable(false);/*  w  ww. j  ava 2  s .com*/
    JScrollPane logScrollPane = new JScrollPane(log);

    //Create a file chooser
    fc = new JFileChooser();

    //Uncomment one of the following lines to try a different
    //file selection mode.  The first allows just directories
    //to be selected (and, at least in the Java look and feel,
    //shown).  The second allows both files and directories
    //to be selected.  If you leave these lines commented out,
    //then the default mode (FILES_ONLY) will be used.
    //
    //fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    //fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

    //Create the open button.  We use the image from the JLF
    //Graphics Repository (but we extracted it from the jar).
    openButton = new JButton("Open a File...", createImageIcon("images/Open16.gif"));
    openButton.addActionListener(this);

    //Create the save button.  We use the image from the JLF
    //Graphics Repository (but we extracted it from the jar).
    saveButton = new JButton("Save a File...", createImageIcon("images/Save16.gif"));
    saveButton.addActionListener(this);

    //For layout purposes, put the buttons in a separate panel
    JPanel buttonPanel = new JPanel(); //use FlowLayout
    buttonPanel.add(openButton);
    buttonPanel.add(saveButton);

    //Add the buttons and the log to this panel.
    add(buttonPanel, BorderLayout.PAGE_START);
    add(logScrollPane, BorderLayout.CENTER);
}