Results 1 to 2 of 2
  1. #1
    Java Tip's Avatar
    Java Tip is offline Moderator
    Join Date
    Nov 2007
    Posts
    1,694
    Blog Entries
    430
    Rep Power
    9

    Default JFileChooser example (selecting a directory)

    The example below shows how to select a directory using a JFileChooser.

    Java Code:
    public class DemoJFileChooser extends JPanel
       implements ActionListener {
       JButton go;
       
       JFileChooser chooser;
       String choosertitle;
       
      public DemoJFileChooser() {
        go = new JButton("Do it");
        go.addActionListener(this);
        add(go);
       }
    
      public void actionPerformed(ActionEvent e) {
        int result;
            
        chooser = new JFileChooser(); 
        chooser.setCurrentDirectory(new java.io.File("."));
        chooser.setDialogTitle(choosertitle);
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        //
        // disable the "All files" option.
        //
        chooser.setAcceptAllFileFilterUsed(false);
        //    
        if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { 
          System.out.println("getCurrentDirectory(): " 
             +  chooser.getCurrentDirectory());
          System.out.println("getSelectedFile() : " 
             +  chooser.getSelectedFile());
          }
        else {
          System.out.println("No Selection ");
          }
         }
       
      public Dimension getPreferredSize(){
        return new Dimension(200, 200);
        }
        
      public static void main(String s[]) {
        JFrame frame = new JFrame("");
        DemoJFileChooser panel = new DemoJFileChooser();
        frame.addWindowListener(
          new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
              System.exit(0);
              }
            }
          );
        frame.getContentPane().add(panel,"Center");
        frame.setSize(panel.getPreferredSize());
        frame.setVisible(true);
        }
    }

  2. #2
    jazz2k8's Avatar
    jazz2k8 is offline Senior Member
    Join Date
    Apr 2008
    Posts
    150
    Rep Power
    7

    Default

    Thanks for your code...

    can't we get this in JSP?? instead of going for swings??

    thanks in advance..

    If so ..need help in this regarding

Similar Threads

  1. Localize JFileChooser
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-14-2008, 11:54 AM
  2. Selecting parts of an image
    By shaungoater in forum Java 2D
    Replies: 1
    Last Post: 12-15-2007, 10:06 PM
  3. JFileChooser remember the location
    By Mr tuition in forum AWT / Swing
    Replies: 3
    Last Post: 12-08-2007, 05:17 PM
  4. selecting a record in database
    By ramachandran in forum New To Java
    Replies: 0
    Last Post: 10-25-2007, 07:06 AM
  5. how to use JFileChooser
    By tommy in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 08:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •