Monday, February 3, 2014

Example of JFileChooser in java.

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

public class JFileChooserTest implements ActionListener
{
    JFileChooserTest()
    {
        JFrame.setDefaultLookAndFeelDecorated(true);
        JDialog.setDefaultLookAndFeelDecorated(true);
        JFrame f = new JFrame("JFileChooser Test");
        f.setLayout(new FlowLayout());
       
        JButton b = new JButton("Select File");
        b.addActionListener(this);
        f.add(b);
       
        f.setSize(300,300);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void actionPerformed(ActionEvent ae)
    {
        JFileChooser fc = new JFileChooser();
        int returnValue = fc.showOpenDialog(null);
        if (returnValue == JFileChooser.APPROVE_OPTION)
        {
            File selectedFile = fc.getSelectedFile();
            System.out.println(selectedFile.getName());
        }
    }
    public static void main(String surat[])
    {
        new JFileChooserTest();
    }
}

Output:

No comments:

Post a Comment