JFileChooser: setCurrentDirectory(File dir) : JFileChooser « javax.swing « Java by API






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;

}

    

   
    
  








Related examples in the same category

1.JFileChooser.APPROVE_OPTION
2.JFileChooser.CANCEL_OPTION
3.JFileChooser.DIRECTORIES_ONLY
4.JFileChooser.DIRECTORY_CHANGED_PROPERTY
5.JFileChooser.ERROR_OPTION
6.JFileChooser.SELECTED_FILE_CHANGED_PROPERTY
7.JFileChooser.SELECTED_FILES_CHANGED_PROPERTY
8.JFileChooser: addActionListener(ActionListener l)
9.JFileChooser: addChoosableFileFilter(FileFilter filter)
10.JFileChooser: addPropertyChangeListener(PropertyChangeListener listener)
11.JFileChooser: createDialog(Component parent)
12.JFileChooser: getCurrentDirectory()
13.JFileChooser: getFileSystemView()
14.JFileChooser: getSelectedFile()
15.JFileChooser: getTypeDescription(File f)
16.JFileChooser: isFileHidingEnabled()
17.JFileChooser: setAccessory(JComponent newAccessory)
18.JFileChooser: setApproveButtonMnemonic(char mnemonic)
19.JFileChooser: setApproveButtonText(String approveButtonText)
20.JFileChooser: setApproveButtonToolTipText(String toolTipText)
21.JFileChooser: setControlButtonsAreShown(boolean b)
22.JFileChooser: setFileFilter(FileFilter filter)
23.JFileChooser: setFileHidingEnabled(boolean b)
24.JFileChooser: setFileSelectionMode(int mode)
25.JFileChooser: setFileView(FileView fileView)
26.JFileChooser: setMultiSelectionEnabled(boolean b)
27.JFileChooser: setSelectedFile(File file)
28.JFileChooser: showOpenDialog(Component parent)