Java by API/javax.swing/JFileChooser — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 14:21, 31 мая 2010

JFileChooser: addActionListener(ActionListener l)

  
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
public class MainClass {
  public static void main(String[] a) {
    JFrame frame = new JFrame("JFileChooser Popup");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JFileChooser fileChooser = new JFileChooser(".");
    fileChooser.setControlButtonsAreShown(false);
    frame.add(fileChooser, BorderLayout.CENTER);
    ActionListener actionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        JFileChooser theFileChooser = (JFileChooser) actionEvent.getSource();
        String command = actionEvent.getActionCommand();
        if (command.equals(JFileChooser.APPROVE_SELECTION)) {
          File selectedFile = theFileChooser.getSelectedFile();
          System.out.println(selectedFile.getParent());
          System.out.println(selectedFile.getName());
        } else if (command.equals(JFileChooser.CANCEL_SELECTION)) {
          System.out.println(JFileChooser.CANCEL_SELECTION);
        }
      }
    };
    fileChooser.addActionListener(actionListener);
    frame.pack();
    frame.setVisible(true);
  }
}





JFileChooser: addChoosableFileFilter(FileFilter filter)

  
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.filechooser.FileFilter;
public class MainClass extends JFrame implements ActionListener {
  MainClass(String title) {
    super(title);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel p = new JPanel();
    JButton jb = new JButton("Open ...");
    jb.addActionListener(this);
    p.add(jb);
    jb = new JButton("Save ...");
    jb.addActionListener(this);
    p.add(jb);
    getContentPane().add(p);
    setSize(200, 65);
    setVisible(true);
  }
  public void actionPerformed(ActionEvent e) {
    int retVal;
    JFileChooser fc = new JFileChooser();
    if (e.getActionCommand().equals("Open ...")) {
      fc.addChoosableFileFilter(new TextFilter());
      retVal = fc.showOpenDialog(this);
    } else
      retVal = fc.showSaveDialog(this);
    if (retVal == JFileChooser.APPROVE_OPTION)
      System.out.println(fc.getSelectedFile().getName());
  }
  public static void main(String[] args) {
    new MainClass("FileChooser Demo");
  }
}
class TextFilter extends FileFilter {
  public boolean accept(File f) {
    if (f.isDirectory())
      return true;
    String s = f.getName();
    int i = s.lastIndexOf(".");
    if (i > 0 && i < s.length() - 1)
      if (s.substring(i + 1).toLowerCase().equals("txt"))
        return true;
    return false;
  }
  public String getDescription() {
    return "Accepts txt files only.";
  }
}





JFileChooser: addPropertyChangeListener(PropertyChangeListener listener)

  
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.Arrays;
import javax.swing.JFileChooser;
public class Main {
  public static void main(String[] argv) {
    JFileChooser chooser = new JFileChooser();
    chooser.addPropertyChangeListener(new PropertyChangeListener() {
      public void propertyChange(PropertyChangeEvent evt) {
        if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(evt.getPropertyName())) {
          JFileChooser chooser = (JFileChooser) evt.getSource();
          File oldFile = (File) evt.getOldValue();
          File newFile = (File) evt.getNewValue();
          System.out.println(oldFile);
          System.out.println(newFile);
          System.out.println(chooser.getSelectedFile());
        } else if (JFileChooser.SELECTED_FILES_CHANGED_PROPERTY.equals(evt.getPropertyName())) {
          JFileChooser chooser = (JFileChooser) evt.getSource();
          File[] oldFiles = (File[]) evt.getOldValue();
          File[] newFiles = (File[]) evt.getNewValue();
          Arrays.toString(oldFiles);
          Arrays.toString(newFiles);
          File[] files = chooser.getSelectedFiles();
          Arrays.toString(files);
        }
      }
    });
    chooser.setVisible(true);
    
  }
}





JFileChooser.APPROVE_OPTION

  
import java.awt.Dimension;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
public class MainClass {
  public static void main(String[] a) {
    JFileChooser fileChooser = new JFileChooser(".");
    fileChooser.setAccessory(new LabelAccessory(fileChooser));
    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");
    }
  }
}
class LabelAccessory extends JLabel implements PropertyChangeListener {
  public LabelAccessory(JFileChooser chooser) {
    chooser.addPropertyChangeListener(this);
    setPreferredSize(new Dimension(100, 100));
  }
  public void propertyChange(PropertyChangeEvent changeEvent) {
    String changeName = changeEvent.getPropertyName();
    if (changeName.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) {
      File file = (File) changeEvent.getNewValue();
      if (file != null) {
        setText(file.getName());
      }
    }
  }
}





JFileChooser.CANCEL_OPTION

  
import java.awt.Dimension;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
public class MainClass {
  public static void main(String[] a) {
    JFileChooser fileChooser = new JFileChooser(".");
    fileChooser.setAccessory(new LabelAccessory(fileChooser));
    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");
    }
  }
}
class LabelAccessory extends JLabel implements PropertyChangeListener {
  public LabelAccessory(JFileChooser chooser) {
    chooser.addPropertyChangeListener(this);
    setPreferredSize(new Dimension(100, 100));
  }
  public void propertyChange(PropertyChangeEvent changeEvent) {
    String changeName = changeEvent.getPropertyName();
    if (changeName.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) {
      File file = (File) changeEvent.getNewValue();
      if (file != null) {
        setText(file.getName());
      }
    }
  }
}





JFileChooser: createDialog(Component parent)

  
import java.awt.ruponent;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.AbstractAction;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
public class Main {
  public static void main(String[] argv) throws Exception {
    
    MyFileChooser chooser = new MyFileChooser();
    chooser.setDialogType(JFileChooser.SAVE_DIALOG);
    final JDialog dialog = chooser.createDialog(null);
    chooser.addActionListener(new AbstractAction() {
      public void actionPerformed(ActionEvent evt) {
        JFileChooser chooser = (JFileChooser) evt.getSource();
        if (JFileChooser.APPROVE_SELECTION.equals(evt.getActionCommand())) {
          dialog.setVisible(false);
        } else if (JFileChooser.CANCEL_SELECTION.equals(evt.getActionCommand())) {
          dialog.setVisible(false);
        }
      }
    });
    dialog.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        dialog.setVisible(false);
      }
    });
    dialog.setVisible(true);
  }
}
class MyFileChooser extends JFileChooser {
  public JDialog createDialog(Component parent) throws HeadlessException {
    return super.createDialog(parent);
  }
}





JFileChooser.DIRECTORIES_ONLY

  

import javax.swing.JFileChooser;
public class Main {
  public static void main(String[] argv) {
    JFileChooser fileChooser = new JFileChooser(".");
    fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  }
}





JFileChooser.DIRECTORY_CHANGED_PROPERTY

  

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import javax.swing.JFileChooser;
public class Main {
  public static void main(String[] argv) throws Exception {
    final JFileChooser chooser = new JFileChooser();
    chooser.addPropertyChangeListener(new PropertyChangeListener() {
      public void propertyChange(PropertyChangeEvent evt) {
        if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(evt.getPropertyName())) {
          JFileChooser chooser = (JFileChooser) evt.getSource();
          File oldDir = (File) evt.getOldValue();
          File newDir = (File) evt.getNewValue();
          File curDir = chooser.getCurrentDirectory();
        }
      }
    });
  }
}





JFileChooser.ERROR_OPTION

  
import javax.swing.JFileChooser;
public class Main {
  public static void main(String[] argv) {
    JFileChooser chooser = new JFileChooser();
    int result = chooser.showOpenDialog(null);
    switch (result) {
    case JFileChooser.APPROVE_OPTION:
      System.out.println("Approve (Open or Save) was clicked");
      break;
    case JFileChooser.CANCEL_OPTION:
      System.out.println("Cancel or the close-dialog icon was clicked");
      break;
    case JFileChooser.ERROR_OPTION:
      System.out.println("Error");
      break;
    }
  }
}





JFileChooser: getCurrentDirectory()

  
import java.io.File;
import javax.swing.JFileChooser;
public class Main {
  public static void main(String[] argv) throws Exception {
    JFileChooser chooser = new JFileChooser();
    File f = new File(new File(".").getCanonicalPath());
    chooser.setCurrentDirectory(f);
    chooser.setCurrentDirectory(null);
    chooser.showOpenDialog(null);
    File curDir = chooser.getCurrentDirectory();
  }
}





JFileChooser: getFileSystemView()

  
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileSystemView;
public class Main {
  public static void main(String[] args) {
    JFileChooser chooser = new JFileChooser();
    FileSystemView view = chooser.getFileSystemView();
    System.out.println("The home directory is " + view.getHomeDirectory());
  }
}





JFileChooser: getSelectedFile()

  
import java.io.File;
import javax.swing.JFileChooser;
public class Main {
  public static void main(String[] a) {
    JFileChooser fileChooser = new JFileChooser(".");
    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("canceled");
    }
  }
}





JFileChooser: getTypeDescription(File f)

  
import java.io.File;
import javax.swing.JFileChooser;
public class Main {
  public static void main(String[] argv) {
    JFileChooser chooser = new JFileChooser();
    File file = new File("filename.txt");
    String fileTypeName = chooser.getTypeDescription(file);
  }
}





JFileChooser: isFileHidingEnabled()

  
 
import javax.swing.JFileChooser;
public class Main {
  public static void main(String[] argv) {
    JFileChooser chooser = new JFileChooser();
    boolean hidingEnabled = chooser.isFileHidingEnabled();
    chooser.setFileHidingEnabled(false);
    chooser.showOpenDialog(null);
  }
}





JFileChooser.SELECTED_FILE_CHANGED_PROPERTY

  
import java.awt.Dimension;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
public class MainClass {
  public static void main(String[] a) {
    JFileChooser fileChooser = new JFileChooser(".");
    fileChooser.setAccessory(new LabelAccessory(fileChooser));
    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");
    }
  }
}
class LabelAccessory extends JLabel implements PropertyChangeListener {
  public LabelAccessory(JFileChooser chooser) {
    chooser.addPropertyChangeListener(this);
    setPreferredSize(new Dimension(100, 100));
  }
  public void propertyChange(PropertyChangeEvent changeEvent) {
    String changeName = changeEvent.getPropertyName();
    if (changeName.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) {
      File file = (File) changeEvent.getNewValue();
      if (file != null) {
        setText(file.getName());
      }
    }
  }
}





JFileChooser.SELECTED_FILES_CHANGED_PROPERTY

  

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import javax.swing.JFileChooser;
public class Main {
  public static void main(String[] argv) throws Exception {
    JFileChooser chooser = new JFileChooser();
    chooser.addPropertyChangeListener(new PropertyChangeListener() {
      public void propertyChange(PropertyChangeEvent evt) {
        if (JFileChooser.SELECTED_FILES_CHANGED_PROPERTY.equals(evt.getPropertyName())) {
          JFileChooser chooser = (JFileChooser) evt.getSource();
          File[] oldFiles = (File[]) evt.getOldValue();
          File[] newFiles = (File[]) evt.getNewValue();
          // Get list of selected files
          File[] files = chooser.getSelectedFiles();
        }    
      }
    });
  }
}





JFileChooser: setAccessory(JComponent newAccessory)

  
import java.awt.Dimension;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
public class MainClass {
  public static void main(String[] a) {
    JFileChooser fileChooser = new JFileChooser(".");
    fileChooser.setAccessory(new LabelAccessory(fileChooser));
    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");
    }
  }
}
class LabelAccessory extends JLabel implements PropertyChangeListener {
  public LabelAccessory(JFileChooser chooser) {
    chooser.addPropertyChangeListener(this);
    setPreferredSize(new Dimension(100, 100));
  }
  public void propertyChange(PropertyChangeEvent changeEvent) {
    String changeName = changeEvent.getPropertyName();
    if (changeName.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) {
      File file = (File) changeEvent.getNewValue();
      if (file != null) {
        setText(file.getName());
      }
    }
  }
}





JFileChooser: setApproveButtonMnemonic(char mnemonic)

  

import javax.swing.JFileChooser;
public class Main {
  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);
  }
}





JFileChooser: setApproveButtonText(String approveButtonText)

  

import javax.swing.JFileChooser;
public class Main {
  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);
  }
}





JFileChooser: setApproveButtonToolTipText(String toolTipText)

  

import javax.swing.JFileChooser;
public class Main {
  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);
  }
}





JFileChooser: setControlButtonsAreShown(boolean b)

  
import java.awt.BorderLayout;
import java.awt.Font;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Main {
  public static void main(String args[]) {
    JFrame frame = new JFrame("JFileChooser Popup");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JLabel directoryLabel = new JLabel(" ");
    directoryLabel.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 36));
    frame.add(directoryLabel, BorderLayout.NORTH);
    final JLabel filenameLabel = new JLabel(" ");
    filenameLabel.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 36));
    frame.add(filenameLabel, BorderLayout.SOUTH);
    JFileChooser fileChooser = new JFileChooser(".");
    fileChooser.setControlButtonsAreShown(false);
    frame.add(fileChooser, BorderLayout.CENTER);
    frame.pack();
    frame.setVisible(true);
  }
}





JFileChooser: setCurrentDirectory(File dir)

  
import java.awt.Container;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.filechooser.FileFilter;
public class Main extends JFrame implements ActionListener {
  public Main() {
    setTitle("ZipTest");
    setSize(300, 400);
    JMenuBar mbar = new JMenuBar();
    JMenu m = new JMenu("File");
    openItem = new JMenuItem("Open");
    openItem.addActionListener(this);
    m.add(openItem);
    exitItem = new JMenuItem("Exit");
    exitItem.addActionListener(this);
    m.add(exitItem);
    mbar.add(m);
    Container contentPane = getContentPane();
    contentPane.add(mbar, "North");
  }
  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();
        }
        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);
  }
  public static void main(String[] args) {
    Frame f = new Main();
    f.setVisible(true);
  }
  private JMenuItem openItem;
  private JMenuItem exitItem;
}





JFileChooser: setFileFilter(FileFilter filter)

  
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
public class MainClass {
  public static void main(String[] a) {
    JFileChooser fileChooser = new JFileChooser(".");
    FileFilter filter1 = new ExtensionFileFilter("JPG and JPEG", new String[] { "JPG", "JPEG" });
    fileChooser.setFileFilter(filter1);
    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);
    }
  }
}
class ExtensionFileFilter extends FileFilter {
  String description;
  String extensions[];
  public ExtensionFileFilter(String description, String extension) {
    this(description, new String[] { extension });
  }
  public ExtensionFileFilter(String description, String extensions[]) {
    if (description == null) {
      this.description = extensions[0];
    } else {
      this.description = description;
    }
    this.extensions = (String[]) extensions.clone();
    toLower(this.extensions);
  }
  private void toLower(String array[]) {
    for (int i = 0, n = array.length; i < n; i++) {
      array[i] = array[i].toLowerCase();
    }
  }
  public String getDescription() {
    return description;
  }
  public boolean accept(File file) {
    if (file.isDirectory()) {
      return true;
    } else {
      String path = file.getAbsolutePath().toLowerCase();
      for (int i = 0, n = extensions.length; i < n; i++) {
        String extension = extensions[i];
        if ((path.endsWith(extension) && (path.charAt(path.length() - extension.length() - 1)) == ".")) {
          return true;
        }
      }
    }
    return false;
  }
}





JFileChooser: setFileHidingEnabled(boolean b)

  
 
import javax.swing.JFileChooser;
public class Main {
  public static void main(String[] argv) {
    JFileChooser chooser = new JFileChooser();
    boolean hidingEnabled = chooser.isFileHidingEnabled();
    chooser.setFileHidingEnabled(false);
    chooser.showOpenDialog(null);
  }
}





JFileChooser: setFileSelectionMode(int mode)

  
import javax.swing.JFileChooser;
public class Main {
  public static void main(String[] a) {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    fileChooser.showOpenDialog(null);
  }
}





JFileChooser: setFileView(FileView fileView)

  
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Graphics;
import java.io.File;
import javax.swing.Icon;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileView;
public class MainClass {
  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");
    }
  }
}
class JavaFileView extends FileView {
  Icon javaIcon = new MyIcon(Color.BLUE);
  Icon classIcon = new MyIcon(Color.GREEN);
  Icon htmlIcon = new MyIcon(Color.RED);
  Icon jarIcon = new MyIcon(Color.PINK);
  public String getName(File file) {
    String filename = file.getName();
    if (filename.endsWith(".java")) {
      String name = filename + " : " + file.length();
      return name;
    }
    return null;
  }
  public String getTypeDescription(File file) {
    String typeDescription = null;
    String filename = file.getName().toLowerCase();
    if (filename.endsWith(".java")) {
      typeDescription = "Java Source";
    } else if (filename.endsWith(".class")) {
      typeDescription = "Java Class File";
    } else if (filename.endsWith(".jar")) {
      typeDescription = "Java Archive";
    } else if (filename.endsWith(".html") || filename.endsWith(".htm")) {
      typeDescription = "Applet Loader";
    }
    return typeDescription;
  }
  public Icon getIcon(File file) {
    if (file.isDirectory()) {
      return null;
    }
    Icon icon = null;
    String filename = file.getName().toLowerCase();
    if (filename.endsWith(".java")) {
      icon = javaIcon;
    } else if (filename.endsWith(".class")) {
      icon = classIcon;
    } else if (filename.endsWith(".jar")) {
      icon = jarIcon;
    } else if (filename.endsWith(".html") || filename.endsWith(".htm")) {
      icon = htmlIcon;
    }
    return icon;
  }
}
class MyIcon implements Icon {
  Color myColor;
  public MyIcon(Color myColor) {
    this.myColor = myColor;
  }
  public int getIconWidth() {
    return 16;
  }
  public int getIconHeight() {
    return 16;
  }
  public void paintIcon(Component c, Graphics g, int x, int y) {
    g.setColor(myColor);
    g.drawRect(0, 0, 16, 16);
  }
}





JFileChooser: setMultiSelectionEnabled(boolean b)

  
import java.io.File;
import javax.swing.JFileChooser;
public class Main {
  public static void main(String[] argv) {
    JFileChooser chooser = new JFileChooser();
    chooser.setMultiSelectionEnabled(true);
    chooser.showOpenDialog(null);
    File[] files = chooser.getSelectedFiles();
  }
}





JFileChooser: setSelectedFile(File file)

  

import java.io.File;
import javax.swing.JFileChooser;
public class Main {
  public static void main(String[] argv) throws Exception{
    JFileChooser chooser = new JFileChooser();
    File f = new File(new File("filename.txt").getCanonicalPath());
    chooser.setSelectedFile(f);
    chooser.showOpenDialog(null);
    File curFile = chooser.getSelectedFile();
  }
}





JFileChooser: showOpenDialog(Component parent)

  
import javax.swing.JFileChooser;
public class Main {
  public static void main(String[] a) {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    fileChooser.showOpenDialog(null);
  }
}