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 void actionPerformed(ActionEvent evt) {
    Object source = evt.getSource();
    if (source == openItem) {
        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new File("."));
        chooser.setFileFilter(new FileFilter() {
            public boolean accept(File f) {
                return f.getName().toLowerCase().endsWith(".zip") || f.isDirectory();
            }//  w w  w . j av a 2  s . com

            public String getDescription() {
                return "ZIP Files";
            }
        });
        int r = chooser.showOpenDialog(this);
        if (r == JFileChooser.APPROVE_OPTION) {
            String zipname = chooser.getSelectedFile().getPath();
            System.out.println(zipname);
        }
    } else if (source == exitItem)
        System.exit(0);
}

From source file:ImageViewer.java

public void actionPerformed(ActionEvent evt) {
    Object source = evt.getSource();
    if (source == openItem) {
        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();
            }//from ww  w.  j a  v  a 2  s  .co  m

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

        int r = chooser.showOpenDialog(this);
        if (r == JFileChooser.APPROVE_OPTION) {
            String name = chooser.getSelectedFile().getName();
            label.setIcon(new ImageIcon(name));
        }
    } else if (source == exitItem)
        System.exit(0);
}

From source file:com.intuit.tank.tools.debugger.SaveTextAction.java

public SaveTextAction(Component parent, String text, String fileName, OutputTextWriter writer) {
    super(text, ActionProducer.getIcon("save_as.png", IconSize.SMALL));
    this.parent = parent;
    this.writer = writer;
    saveAsChooser = new JFileChooser();
    saveAsChooser.setDialogTitle(text);/*from ww  w.  ja  va 2 s . c  o  m*/
    // saveAsChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    saveAsChooser.setSelectedFile(new File(fileName));
}

From source file:net.fabricmc.installer.installer.LocalVersionInstaller.java

public static void installServer(File mcDir, IInstallerProgress progress) throws Exception {
    JFileChooser fc = new JFileChooser();
    fc.setDialogTitle(Translator.getString("install.client.selectCustomJar"));
    fc.setFileFilter(new FileNameExtensionFilter("Jar Files", "jar"));
    if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        File inputFile = fc.getSelectedFile();

        JarFile jarFile = new JarFile(inputFile);
        Attributes attributes = jarFile.getManifest().getMainAttributes();
        String fabricVersion = attributes.getValue("FabricVersion");
        jarFile.close();/*from  w ww.  java 2s  .com*/
        File fabricJar = new File(mcDir, "fabric-" + fabricVersion + ".jar");
        if (fabricJar.exists()) {
            fabricJar.delete();
        }
        FileUtils.copyFile(inputFile, fabricJar);
        ServerInstaller.install(mcDir, fabricVersion, progress, fabricJar);
    } else {
        throw new Exception("Failed to find jar");
    }

}

From source file:MyFilterChooser.java

public MyFilterChooser() {
    super("Filter Test Frame");
    setSize(350, 200);//from   w  ww .jav  a 2s.  c om
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    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) {
            String[] pics = new String[] { "gif", "jpg", "tif" };
            String[] audios = new String[] { "au", "aiff", "wav" };
            JFileChooser chooser = new JFileChooser();
            chooser.addChoosableFileFilter(new SimpleFileFilter(pics, "Images (*.gif, *.jpg, *.tif)"));
            chooser.addChoosableFileFilter(new SimpleFileFilter(".MOV"));
            chooser.addChoosableFileFilter(new SimpleFileFilter(audios, "Sounds (*.aiff, *.au, *.wav)"));
            int option = chooser.showOpenDialog(MyFilterChooser.this);
            if (option == JFileChooser.APPROVE_OPTION) {
                if (chooser.getSelectedFile() != null)
                    statusbar.setText("You chose " + chooser.getSelectedFile().getName());
            } else {
                statusbar.setText("You canceled.");
            }
        }
    });

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

From source file:MenuDemo.java

public MenuDemo() {
    super("Basic text editor");
    setSize(450, 350);/* w  w  w . j a  v  a 2s  .  c  o m*/

    fontArray = new Font[FontNames.length];
    for (int i = 0; i < FontNames.length; i++)
        fontArray[i] = new Font(FontNames[i], Font.PLAIN, 12);

    JMenuBar menuBar = createMenuBar();
    setJMenuBar(menuBar);

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

    WindowListener exitEvent = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    addWindowListener(exitEvent);

    updateMonitor();
    setVisible(true);
}

From source file:SimpleFileChooser.java

public SimpleFileChooser() {
    super("File Chooser Test Frame");
    setSize(350, 200);//from w  w w .  j  a  v  a 2 s  .co m
    setDefaultCloseOperation(EXIT_ON_CLOSE);

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

    JButton openButton = new JButton("Open");
    JButton saveButton = new JButton("Save");
    JButton dirButton = new JButton("Pick Dir");
    final JLabel statusbar = new JLabel("Output of your selection will go here");

    // Create a file chooser that opens up as an Open dialog
    openButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            JFileChooser chooser = new JFileChooser();
            chooser.setMultiSelectionEnabled(true);
            int option = chooser.showOpenDialog(SimpleFileChooser.this);
            if (option == JFileChooser.APPROVE_OPTION) {
                File[] sf = chooser.getSelectedFiles();
                String filelist = "nothing";
                if (sf.length > 0)
                    filelist = sf[0].getName();
                for (int i = 1; i < sf.length; i++) {
                    filelist += ", " + sf[i].getName();
                }
                statusbar.setText("You chose " + filelist);
            } else {
                statusbar.setText("You canceled.");
            }
        }
    });

    // Create a file chooser that opens up as a Save dialog
    saveButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            JFileChooser chooser = new JFileChooser();
            int option = chooser.showSaveDialog(SimpleFileChooser.this);
            if (option == JFileChooser.APPROVE_OPTION) {
                statusbar.setText("You saved "
                        + ((chooser.getSelectedFile() != null) ? chooser.getSelectedFile().getName()
                                : "nothing"));
            } else {
                statusbar.setText("You canceled.");
            }
        }
    });

    // Create a file chooser that allows you to pick a directory
    // rather than a file
    dirButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            JFileChooser chooser = new JFileChooser();
            chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int option = chooser.showOpenDialog(SimpleFileChooser.this);
            if (option == JFileChooser.APPROVE_OPTION) {
                statusbar.setText("You opened "
                        + ((chooser.getSelectedFile() != null) ? chooser.getSelectedFile().getName()
                                : "nothing"));
            } else {
                statusbar.setText("You canceled.");
            }
        }
    });

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

From source file:Main.java

public Main() {

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    iconLabel = new JLabel("Please select an image.");
    iconLabel.setHorizontalAlignment(SwingConstants.CENTER);

    fileChooser = new JFileChooser();

    frame.add(iconLabel, BorderLayout.CENTER);
    frame.add(new JButton(new SelectImageAction("Select Image...")), BorderLayout.SOUTH);
    frame.setBounds(16, 16, 640, 480);//ww w .ja  v a 2 s.  c  o  m
    frame.setVisible(true);
}

From source file:ImageFileFilterImageScale.java

private String getImageFile() {
    JFileChooser fc = new JFileChooser();
    fc.setFileFilter(new ImageFilter());
    int result = fc.showOpenDialog(null);
    File file = null;//from www  . j  ava  2s  .c o  m
    if (result == JFileChooser.APPROVE_OPTION) {
        file = fc.getSelectedFile();
        return file.getPath();
    } else
        return null;

}

From source file:id.co.nlp.MachineTranslationAPP.FormTraining.java

public FormTraining(FormTesting formTesting) {
    this.formTesting = formTesting;
    this.fileChooser = new JFileChooser();
    this.machineTranslation = new MachineTranslation();
    initComponents();/*from  w  ww. jav a  2  s.  com*/
    this.jMenu1.setVisible(false);
}