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:Main.java

public static void main(String[] args) throws Exception {
    String[] properties = { "os.name", "java.version", "java.vm.version", "java.vendor" };
    for (String property : properties) {
        System.out.println(property + ": " + System.getProperty(property));
    }//from w  w  w.jav a  2  s.com
    JFileChooser jfc = new JFileChooser();
    jfc.showOpenDialog(null);
    jfc.addChoosableFileFilter(new FileFilter() {
        @Override
        public boolean accept(File f) {
            return f.isDirectory() || f.getName().toLowerCase().endsWith(".obj");
        }

        @Override
        public String getDescription() {
            return "Wavefront OBJ (*.obj)";
        }

        @Override
        public String toString() {
            return getDescription();
        }
    });
    int result = JOptionPane.showConfirmDialog(null, "Description was 'All Files'?");
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    SwingUtilities.updateComponentTreeUI(jfc);
    jfc.showOpenDialog(null);
    result = JOptionPane.showConfirmDialog(null, "Description was 'All Files'?");

    result = JOptionPane.showConfirmDialog(null, "Description was 'All Files'?");
    for (UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
        if ("Nimbus".equals(info.getName())) {
            UIManager.setLookAndFeel(info.getClassName());
            SwingUtilities.updateComponentTreeUI(jfc);
            break;
        }
    }
    jfc.showOpenDialog(null);
    result = JOptionPane.showConfirmDialog(null, "Description was 'All Files'?");
}

From source file:ActionListenerTest3.java

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

    JButton button = new JButton("Select File");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JFileChooser fileChooser = new JFileChooser();
            int returnVal = fileChooser.showOpenDialog(null);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                System.out.println(fileChooser.getSelectedFile().getName());
            }//from   w w w.j a  va 2 s.c  o  m
        }
    });

    frame.add(button);
    frame.pack();
    frame.setVisible(true);
}

From source file:JFileChooserTest.java

public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JDialog.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("JComboBox Test");
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton button = new JButton("Select File");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            JFileChooser fileChooser = new JFileChooser();
            int returnValue = fileChooser.showOpenDialog(null);
            if (returnValue == JFileChooser.APPROVE_OPTION) {
                File selectedFile = fileChooser.getSelectedFile();
                System.out.println(selectedFile.getName());
            }/* w  w  w.  j  ava  2 s  .  com*/
        }
    });
    frame.add(button);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String s[]) {
    JFileChooser chooser = new JFileChooser();
    disableNewFolderButton(chooser);/*from   w  w w . j  av  a 2s . c o  m*/
    int rc = chooser.showOpenDialog(null);
}

From source file:Main.java

public static void main(String[] args) {
    // Create a file filter to show only a directory or .doc files
    FileFilter filter = new FileFilter() {
        @Override//ww  w .j a v  a  2 s .co  m
        public boolean accept(File f) {
            if (f.isDirectory()) {
                return true;
            }

            String fileName = f.getName().toLowerCase();
            if (fileName.endsWith(".doc")) {
                return true;
            }

            return false; // Reject any other files
        }

        @Override
        public String getDescription() {
            return "Word Document";
        }
    };

    // Set the file filter
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileFilter(filter);

    int returnValue = fileChooser.showDialog(null, "Attach");
    if (returnValue == JFileChooser.APPROVE_OPTION) {
        // Process the file
    }

}

From source file:Main.java

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

    f.setSize(300, 500);//from  www . j  a  v  a  2  s .com
    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:JavaFileView.java

public static void main(String[] a) {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileView(new JavaFileView());
    fileChooser.showOpenDialog(null);//from w  w w .  j a  v a 2 s . co m
}

From source file:PersistentFrameTest.java

 public static void main(String[] args)
{
   chooser = new JFileChooser();
   chooser.setCurrentDirectory(new File("."));      
   PersistentFrameTest test = new PersistentFrameTest();
   test.init();//  ww  w.  ja v  a 2 s.  co m
}

From source file:DesktopDemo.java

public static void main(String[] args) {

    if (Desktop.isDesktopSupported()) {
        desktop = Desktop.getDesktop();
    } else {// w w  w.  j av a2s .com
        System.out.println("Desktop class is not supported");
        System.exit(1);
    }
    JMenuItem openItem = new JMenuItem("Open");
    JMenuItem editItem = new JMenuItem("Edit");
    JMenuItem printItem = new JMenuItem("Print");
    JMenuItem browseToItem = new JMenuItem("Go to www.java2s.com");
    JMenuItem mailToItem = new JMenuItem("Email to a@java.com");
    JMenu fileMenu = new JMenu("File");
    JMenu mailMenu = new JMenu("Email");
    JMenu browseMenu = new JMenu("Browser");

    openItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser();
            if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                try {
                    desktop.open(chooser.getSelectedFile().getAbsoluteFile());
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    });
    fileMenu.add(openItem);

    editItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser();
            if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                try {
                    desktop.edit(chooser.getSelectedFile().getAbsoluteFile());
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    });
    fileMenu.add(editItem);

    printItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser();
            if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                try {
                    desktop.print(chooser.getSelectedFile().getAbsoluteFile());
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    });
    fileMenu.add(printItem);

    browseToItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                URI browseURI = new URI("www.java2s.com");
                desktop.browse(browseURI);
            } catch (Exception ex) {
                System.out.println(ex.getMessage());
            }
        }
    });
    browseMenu.add(browseToItem);

    mailToItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                URI mailURI = new URI("mailto:support@java.com");
                desktop.mail(mailURI);
            } catch (Exception ex) {
                System.out.println(ex.getMessage());
            }
        }
    });
    mailMenu.add(mailToItem);

    JMenuBar jMenuBar = new JMenuBar();
    jMenuBar.add(fileMenu);
    jMenuBar.add(browseMenu);
    jMenuBar.add(mailMenu);

    JFrame frame = new JFrame();
    frame.setTitle("Desktop Helper Applications");
    frame.setSize(300, 100);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setJMenuBar(jMenuBar);
    frame.setVisible(true);

}

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);//  w  w w  .  jav  a  2s  .c  o  m
    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);
    }
}