Example usage for javax.swing JFileChooser setFileView

List of usage examples for javax.swing JFileChooser setFileView

Introduction

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

Prototype

@BeanProperty(preferred = true, description = "Sets the File View used to get file type information.")
public void setFileView(FileView fileView) 

Source Link

Document

Sets the file view to be used to retrieve UI information, such as the icon that represents a file or the type description of a file.

Usage

From source file:JavaFileView.java

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

From source file:MainClass.java

public static void main(String[] a) {
    JFileChooser fileChooser = new JFileChooser(".");
    FileView view = new JavaFileView();
    fileChooser.setFileView(view);
    int status = fileChooser.showOpenDialog(null);
    if (status == JFileChooser.APPROVE_OPTION) {
        File selectedFile = fileChooser.getSelectedFile();
        System.out.println(selectedFile.getParent());
        System.out.println(selectedFile.getName());
    } else if (status == JFileChooser.CANCEL_OPTION) {
        System.out.println("JFileChooser.CANCEL_OPTION");
    }//from   w w w  . ja v  a 2 s.com
}

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(" ");
            }/*from   ww w.j  av a 2s. c o m*/
        }
    };
    button.addActionListener(actionListener);
    contentPane.add(button, BorderLayout.CENTER);

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

From source file:FilterSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("JFileChooser Filter 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));
            FileFilter filter1 = new ExtensionFileFilter(null, new String[] { "JPG", "JPEG" });
            //        fileChooser.setFileFilter(filter1);
            fileChooser.addChoosableFileFilter(filter1);
            FileFilter filter2 = new ExtensionFileFilter("gif", new String[] { "gif" });
            fileChooser.addChoosableFileFilter(filter2);
            fileChooser.setFileView(new JavaFileView());
            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  .  ja  v  a2 s  . c  om*/
        }
    };
    button.addActionListener(actionListener);
    contentPane.add(button, BorderLayout.CENTER);

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

From source file:MyViewChooser.java

public MyViewChooser() {
    super("File View Test Frame");
    setSize(350, 200);/*from w w w .j  a  va  2  s .  co m*/
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    parent = this;

    Container c = getContentPane();
    c.setLayout(new FlowLayout());

    JButton openButton = new JButton("Open");
    final JLabel statusbar = new JLabel("Output of your selection will go here");

    openButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            JFileChooser chooser = new JFileChooser();

            // Ok, set up our own file view for the chooser
            chooser.setFileView(new ThumbNailFileView(MyViewChooser.this));

            int option = chooser.showOpenDialog(parent);
            if (option == JFileChooser.APPROVE_OPTION) {
                statusbar.setText("You chose " + chooser.getSelectedFile().getName());
            } else {
                statusbar.setText("You cancelled.");
            }
        }
    });

    c.add(openButton);
    c.add(statusbar);
}

From source file:EnhancedFileTester.java

public EnhancedFileTester() {
    JButton jb = new JButton("Open File Viewer");
    add(jb);/*w  ww  .j av a  2  s  .com*/
    ActionListener listener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JFileChooser chooser = new JFileChooser(".");
            FileFilter type1 = new ExtensionFilter("Java source", ".java");
            FileFilter type2 = new ExtensionFilter("Image files",
                    new String[] { ".jpg", ".gif", "jpeg", "xbm" });
            FileFilter type3 = new ExtensionFilter("HTML files", new String[] { ".htm", ".html" });
            chooser.addChoosableFileFilter(type1);
            chooser.addChoosableFileFilter(type2);
            chooser.addChoosableFileFilter(type3);
            chooser.setFileFilter(type2); // Initial filter setting
            FileView view = new IconView();
            chooser.setFileView(view);
            int status = chooser.showOpenDialog(EnhancedFileTester.this);
            if (status == JFileChooser.APPROVE_OPTION) {
                File f = chooser.getSelectedFile();
                System.out.println(f);
            }
        }
    };
    jb.addActionListener(listener);
}

From source file:com.igormaznitsa.sciareto.ui.MainFrame.java

private void menuOpenProjectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuOpenProjectActionPerformed
    final JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileView(new FileView() {
        private Icon KNOWLEDGE_FOLDER_ICO = null;

        @Override/*from w ww .  j a  v a 2  s  . c  o m*/
        public Icon getIcon(final File f) {
            if (f.isDirectory()) {
                final File knowledge = new File(f, ".projectKnowledge");
                if (knowledge.isDirectory()) {
                    if (KNOWLEDGE_FOLDER_ICO == null) {
                        final Icon icon = UIManager.getIcon("FileView.directoryIcon");
                        if (icon != null) {
                            KNOWLEDGE_FOLDER_ICO = new ImageIcon(
                                    UiUtils.makeBadgedRightBottom(UiUtils.iconToImage(fileChooser, icon),
                                            Icons.MMDBADGE.getIcon().getImage()));
                        }
                    }
                    return KNOWLEDGE_FOLDER_ICO;
                } else {
                    return super.getIcon(f);
                }
            } else if (f.isFile() && f.getName().toLowerCase(Locale.ENGLISH).endsWith(".mmd")) {
                return Icons.DOCUMENT.getIcon();
            } else {
                return super.getIcon(f);
            }
        }
    });
    fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    fileChooser.setMultiSelectionEnabled(false);
    fileChooser.setDialogTitle("Open project folder");

    if (fileChooser.showOpenDialog(Main.getApplicationFrame()) == JFileChooser.APPROVE_OPTION) {
        openProject(fileChooser.getSelectedFile(), false);
    }
}

From source file:org.domainmath.gui.MainFrame.java

/**
 * Creates file chooser dialog box and allows the user to select files
 * and importData selected files in fileTab.
 *//*from w ww . j  a  v a  2s . com*/
public void open() {
    JFileChooser fc = new JFileChooser();

    // set current directory.
    if (fileTab.getTabCount() > 0) {
        File f = new File(fileTab.getToolTipTextAt(fileTab.getSelectedIndex()));
        fc.setCurrentDirectory(f.getParentFile());
    } else {
        fc.setCurrentDirectory(new File(System.getProperty("user.dir") + File.separator + "works"));
    }

    FileNameExtensionFilter filter = new FileNameExtensionFilter("M-Files  (*.m)", "m");

    fc.setAcceptAllFileFilterUsed(false);

    fc.setFileFilter(filter);
    fc.setMultiSelectionEnabled(true);
    fc.setFileView(new ScriptFileView());
    File file1[];
    int returnVal = fc.showOpenDialog(this);

    if (returnVal == JFileChooser.APPROVE_OPTION) {
        file1 = fc.getSelectedFiles();

        this.setCurrentDirFileTab(fc.getCurrentDirectory().getAbsolutePath());
        for (int i = 0; i < file1.length; i++) {
            if (!fileNameList.contains(file1[i].getAbsolutePath())) {
                open(file1[i], i);

            } else {
                System.out.println(file1[i].getAbsolutePath() + " already open!");
            }
        }
    }
}

From source file:org.panbox.desktop.common.gui.RestoreRevisionDialog.java

private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_browseButtonActionPerformed
    JFileChooser fileChooser = new JFileChooser(PANBOX_DIR + "/" + shareName);
    fileChooser.setFileView(new FileView() {
        @Override/*from www .  ja  v a 2s .  c o  m*/
        public Boolean isTraversable(File f) {
            try {
                return f.isDirectory()
                        && FilenameUtils.directoryContains(PANBOX_DIR.getAbsolutePath(), f.getAbsolutePath());
            } catch (IOException e) {
                logger.error("Error determining folder parent status!");
                return true;
            }
        }
    });
    fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    int ret = fileChooser.showOpenDialog(this);
    if (ret == JFileChooser.APPROVE_OPTION && fileChooser.getSelectedFile().getParentFile().getAbsolutePath()
            .startsWith(PANBOX_DIR.getAbsolutePath())) {
        String path = getCSPSupportedPath(fileChooser.getSelectedFile().getPath());
        fileTextField.setText(path);
        fillRevisionsForFileName(path);
    }
}

From source file:umich.ms.batmass.filesupport.core.actions.importing.ImportFileByCategory.java

/**
 * /*from w w w.ja  v  a  2  s.  co  m*/
 * @param fcBuilder
 * @return 
 */
protected File[] showMultiOpenDialog(FileChooserBuilder fcBuilder) {
    JFileChooser chooser = fcBuilder.createFileChooser();
    BMFileView bmFileView = new BMFileView(getFileCategory());
    chooser.setFileView(bmFileView);
    chooser.setMultiSelectionEnabled(true);
    int result = chooser.showOpenDialog(findDialogParent());
    if (JFileChooser.APPROVE_OPTION == result) {
        File[] files = chooser.getSelectedFiles();
        return files == null ? new File[0] : files;
    } else {
        return null;
    }
}