Monday, February 3, 2014

Another example of JFileChooser

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class filechooser extends JFrame implements ActionListener
{
    JFileChooser jfc;
   
    public filechooser()
    {
        super("File Chooser");
        jfc=new JFileChooser("e:/");
        JButton jb=new JButton("Ok");
        jb.addActionListener(this);
        getContentPane().add(jb);
        setLayout(new FlowLayout());
        setSize(300,300);
        setVisible(true);
    }
   
    public void actionPerformed(ActionEvent e)
    {
        int x=jfc.showOpenDialog(null);
       
        if(x==JFileChooser.APPROVE_OPTION)
        {
            File f=jfc.getSelectedFile();
            String s=jfc.getName(f);
            System.out.println(s);
        }
       
        if(x==JFileChooser.CANCEL_OPTION)
        {
            System.out.println("Cancel");
        }
    }
    public static void main(String surat[])
    {
        new filechooser();
    }
}

Output:

No comments:

Post a Comment