Example usage for javax.swing JFileChooser showOpenDialog

List of usage examples for javax.swing JFileChooser showOpenDialog

Introduction

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

Prototype

public int showOpenDialog(Component parent) throws HeadlessException 

Source Link

Document

Pops up an "Open File" file chooser dialog.

Usage

From source file:Main.java

public static void main(String[] args) {
    final JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JButton loadButton = new JButton("Display Image");
    loadButton.addActionListener(ev -> {
        JFileChooser fc = new JFileChooser(System.getProperty("user.home"));
        fc.addChoosableFileFilter(/*from w w  w. j  a va2s.co  m*/
                new FileNameExtensionFilter("Image files", new String[] { "png", "jpg", "jpeg", "gif" }));
        if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
            try {
                Image image = ImageIO.read(fc.getSelectedFile());
                if (image != null) {
                    JPanel panel = new JPanel(new BorderLayout(10, 10));
                    panel.add(new JLabel(fc.getSelectedFile().toString()), BorderLayout.NORTH);
                    panel.add(new JLabel(new ImageIcon(image)));
                    JOptionPane.showMessageDialog(frame, panel);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    });

    frame.add(loadButton);
    frame.pack();
    frame.setLocationByPlatform(true);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String s[]) {
    JFileChooser chooser = new JFileChooser();
    chooser.setCurrentDirectory(new java.io.File("."));
    chooser.setDialogTitle("choosertitle");
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    chooser.setAcceptAllFileFilterUsed(false);

    if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory());
        System.out.println("getSelectedFile() : " + chooser.getSelectedFile());
    } else {//  w  w w  .j  ava 2 s.co m
        System.out.println("No Selection ");
    }
}

From source file:Main.java

public static void main(String[] args) {
    JFrame f = new JFrame();

    f.setSize(300, 500);//w  ww .jav a 2s .co m
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel pan = new JPanel(new GridLayout(1, 1));
    XmlJTree myTree = new XmlJTree(null);
    f.add(new JScrollPane(myTree));
    JButton button = new JButton("Choose file");
    button.addActionListener(e -> {
        JFileChooser chooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter("XML file", "xml");
        chooser.setFileFilter(filter);
        int returnVal = chooser.showOpenDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            myTree.setPath(chooser.getSelectedFile().getAbsolutePath());
        }
    });
    pan.add(button);
    f.add(pan, BorderLayout.SOUTH);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) {
    JFileChooser chooser = new JFileChooser();
    // Set the text
    chooser.setApproveButtonText("New Approve Text");
    // Set the mnemonic
    chooser.setApproveButtonMnemonic('a');
    // Set the tool tip
    chooser.setApproveButtonToolTipText("New Approve Tool Tip");
    chooser.showOpenDialog(null);
}

From source file:Main.java

public static void main(String[] argv) {
    JFileChooser chooser = new JFileChooser();
    // Set the text
    chooser.setApproveButtonText("New Approve Text");
    // Set the mnemonic
    chooser.setApproveButtonMnemonic((int) 'a');
    // Set the tool tip
    chooser.setApproveButtonToolTipText("New Approve Tool Tip");
    chooser.showOpenDialog(null);
}

From source file:AudioFilter.java

public static void main(String[] args) {
    AudioFilter audioFilter = new AudioFilter();

    JFileChooser jfc = new JFileChooser();

    jfc.setDialogTitle("Open File");
    jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
    jfc.setCurrentDirectory(new File("."));
    jfc.setFileFilter(audioFilter);//from  w w  w.j a  v  a  2 s  .c om
    int result = jfc.showOpenDialog(null);

    if (result == JFileChooser.CANCEL_OPTION) {
        System.out.println("cancel");
    } else if (result == JFileChooser.APPROVE_OPTION) {
        File fFile = jfc.getSelectedFile();
        String filestr = fFile.getAbsolutePath();

        System.out.println(filestr);
    }
}

From source file:com.novadart.silencedetect.SilenceDetect.java

public static void main(String[] args) {

    // create the parser
    CommandLineParser parser = new BasicParser();
    try {//from w  ww  . j a va2 s.  c  o  m
        // parse the command line arguments
        CommandLine line = parser.parse(OPTIONS, args);

        if (line.hasOption("h")) {

            printHelp();

        } else {

            String decibels = "-10";
            if (line.hasOption("b")) {
                decibels = "-" + line.getOptionValue("b");
            } else {
                throw new RuntimeException();
            }

            String videoFile = null;

            if (line.hasOption("i")) {

                videoFile = line.getOptionValue("i");

                Boolean debug = line.hasOption("d");

                if (line.hasOption("t")) {

                    System.out.println(printAudioTracksList(videoFile, debug));

                } else if (line.hasOption("s")) {

                    int trackNumber = Integer.parseInt(line.getOptionValue("s"));
                    System.out.println(printAudioTrackSilenceDuration(videoFile, trackNumber, decibels, debug));

                } else {

                    printHelp();

                }

            } else if (line.hasOption("-j")) {

                // choose file
                final JFileChooser fc = new JFileChooser();
                fc.setVisible(true);
                int returnVal = fc.showOpenDialog(null);
                if (returnVal == JFileChooser.APPROVE_OPTION) {

                    videoFile = fc.getSelectedFile().getAbsolutePath();

                } else {
                    return;
                }

                JTextArea tracks = new JTextArea();
                tracks.setText(printAudioTracksList(videoFile, true));
                JSpinner trackNumber = new JSpinner();
                trackNumber.setValue(1);
                final JComponent[] inputs = new JComponent[] { new JLabel("Audio Tracks"), tracks,
                        new JLabel("Track to analyze"), trackNumber };
                JOptionPane.showMessageDialog(null, inputs, "Select Audio Track", JOptionPane.PLAIN_MESSAGE);

                JTextArea results = new JTextArea();
                results.setText(printAudioTrackSilenceDuration(videoFile, (int) trackNumber.getValue(),
                        decibels, false));
                final JComponent[] resultsInputs = new JComponent[] { new JLabel("Results"), results };
                JOptionPane.showMessageDialog(null, resultsInputs, "RESULTS!", JOptionPane.PLAIN_MESSAGE);

            } else {
                printHelp();
                return;
            }

        }

    } catch (ParseException | IOException | InterruptedException exp) {
        // oops, something went wrong
        System.out.println("There was a problem :(\nReason: " + exp.getMessage());
    }
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    JFileChooser fc = new JFileChooser();
    fc.setMultiSelectionEnabled(true);//from  w w w  .j a v a2s .  c  om
    fc.setCurrentDirectory(new File("C:\\tmp"));
    JButton btn1 = new JButton("Show Dialog");
    btn1.addActionListener(e -> fc.showDialog(frame, "Choose"));
    JButton btn2 = new JButton("Show Open Dialog");
    btn2.addActionListener(e -> {
        int retVal = fc.showOpenDialog(frame);
        if (retVal == JFileChooser.APPROVE_OPTION) {
            File[] selectedfiles = fc.getSelectedFiles();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < selectedfiles.length; i++) {
                sb.append(selectedfiles[i].getName());
                sb.append("\n");
            }
            JOptionPane.showMessageDialog(frame, sb.toString());
        }
    });
    JButton btn3 = new JButton("Show Save Dialog");
    btn3.addActionListener(e -> fc.showSaveDialog(frame));
    Container pane = frame.getContentPane();
    pane.setLayout(new GridLayout(3, 1, 10, 10));
    pane.add(btn1);
    pane.add(btn2);
    pane.add(btn3);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

From source file:FileFilterDemo.java

public static void main(String[] args) {
    JFileChooser chooser = new JFileChooser();
    chooser.setCurrentDirectory(new File("."));

    chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
        public boolean accept(File f) {
            return f.getName().toLowerCase().endsWith(".gif") || f.isDirectory();
        }//w w  w . j ava2  s.c o  m

        public String getDescription() {
            return "GIF Images";
        }
    });

    int r = chooser.showOpenDialog(new JFrame());
    if (r == JFileChooser.APPROVE_OPTION) {
        String name = chooser.getSelectedFile().getName();
        System.out.println(name);
    }
}

From source file:FileSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("JFileChooser Popup");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    final JLabel directoryLabel = new JLabel();
    contentPane.add(directoryLabel, BorderLayout.NORTH);

    final JLabel filenameLabel = new JLabel();
    contentPane.add(filenameLabel, BorderLayout.SOUTH);

    final JButton button = new JButton("Open FileChooser");
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Component parent = (Component) actionEvent.getSource();
            JFileChooser fileChooser = new JFileChooser(".");
            fileChooser.setAccessory(new LabelAccessory(fileChooser));
            FileView view = new JavaFileView();
            fileChooser.setFileView(view);
            int status = fileChooser.showOpenDialog(parent);
            if (status == JFileChooser.APPROVE_OPTION) {
                File selectedFile = fileChooser.getSelectedFile();
                directoryLabel.setText(selectedFile.getParent());
                filenameLabel.setText(selectedFile.getName());
            } else if (status == JFileChooser.CANCEL_OPTION) {
                directoryLabel.setText(" ");
                filenameLabel.setText(" ");
            }/*w  w w.  j  a  v  a 2s. c  o m*/
        }
    };
    button.addActionListener(actionListener);
    contentPane.add(button, BorderLayout.CENTER);

    frame.setSize(300, 200);
    frame.setVisible(true);
}