Advertisement
coasterka

SimpleFileChooser

May 6th, 2014
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.02 KB | None | 0 0
  1. // SimpleFileChooser.java
  2. // A simple file chooser to see what it takes to make one of these work.
  3. //
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import java.io.*;
  7. import javax.swing.*;
  8.  
  9. public class SimpleFileChooser extends JFrame {
  10.  
  11.    public SimpleFileChooser() {
  12.     super("File Chooser Test Frame");
  13.     setSize(350, 200);
  14.     setDefaultCloseOperation(EXIT_ON_CLOSE);
  15.  
  16.     Container c = getContentPane();
  17.     c.setLayout(new FlowLayout());
  18.    
  19.     JButton openButton = new JButton("Open");
  20.     JButton saveButton = new JButton("Save");
  21.     JButton dirButton = new JButton("Pick Dir");
  22.     final JLabel statusbar =
  23.                  new JLabel("Output of your selection will go here");
  24.  
  25.     // Create a file chooser that opens up as an Open dialog
  26.     openButton.addActionListener(new ActionListener() {
  27.       public void actionPerformed(ActionEvent ae) {
  28.         JFileChooser chooser = new JFileChooser();
  29.         chooser.setMultiSelectionEnabled(true);
  30.         int option = chooser.showOpenDialog(SimpleFileChooser.this);
  31.         if (option == JFileChooser.APPROVE_OPTION) {
  32.           File[] sf = chooser.getSelectedFiles();
  33.           String filelist = "nothing";
  34.           if (sf.length > 0) filelist = sf[0].getName();
  35.           for (int i = 1; i < sf.length; i++) {
  36.             filelist += ", " + sf[i].getName();
  37.           }
  38.           statusbar.setText("You chose " + filelist);
  39.         }
  40.         else {
  41.           statusbar.setText("You canceled.");
  42.         }
  43.       }
  44.     });
  45.  
  46.     // Create a file chooser that opens up as a Save dialog
  47.     saveButton.addActionListener(new ActionListener() {
  48.       public void actionPerformed(ActionEvent ae) {
  49.         JFileChooser chooser = new JFileChooser();
  50.         int option = chooser.showSaveDialog(SimpleFileChooser.this);
  51.         if (option == JFileChooser.APPROVE_OPTION) {
  52.           statusbar.setText("You saved " + ((chooser.getSelectedFile()!=null)?
  53.                             chooser.getSelectedFile().getName():"nothing"));
  54.         }
  55.         else {
  56.           statusbar.setText("You canceled.");
  57.         }
  58.       }
  59.     });
  60.  
  61.     // Create a file chooser that allows you to pick a directory
  62.     // rather than a file
  63.     dirButton.addActionListener(new ActionListener() {
  64.       public void actionPerformed(ActionEvent ae) {
  65.         JFileChooser chooser = new JFileChooser();
  66.         chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  67.         int option = chooser.showOpenDialog(SimpleFileChooser.this);
  68.         if (option == JFileChooser.APPROVE_OPTION) {
  69.           statusbar.setText("You opened " + ((chooser.getSelectedFile()!=null)?
  70.                             chooser.getSelectedFile().getName():"nothing"));
  71.         }
  72.         else {
  73.           statusbar.setText("You canceled.");
  74.         }
  75.       }
  76.     });
  77.  
  78.     c.add(openButton);
  79.     c.add(saveButton);
  80.     c.add(dirButton);
  81.     c.add(statusbar);
  82.   }
  83.  
  84.   public static void main(String args[]) {
  85.     SimpleFileChooser sfc = new SimpleFileChooser();
  86.     sfc.setVisible(true);
  87.   }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement