import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class FileChooser extends JPanel implements ActionListener
{
	JButton openButton, saveButton;
    JTextArea log;
    JFileChooser theChooser;
 
    public FileChooser()
    {
        super(new BorderLayout());
 
        theChooser = new JFileChooser();  //This is the code for the file chooser (seriously)
        
        //Makes the text area
        log = new JTextArea(15,60);
        log.setMargin(new Insets(10,10,10,10));
        log.setEditable(false);
 
        //tells the chooser what types of things to look for (the default is files only)
       // theChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        //theChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
 
        //---------------------------------------------------------//
        //Creates the open button.  								//		  
        openButton = new JButton("Open a File (but not really)");    //
        openButton.addActionListener(this);          				  //
                                                                       //
        saveButton = new JButton("Save a File (but not really)");	    //
        saveButton.addActionListener(this);								 //
        																  //
        //For layout purposes, put the buttons in a separate panel         --------This isn't part of file chooser
        JPanel buttonPanel = new JPanel();								  //
        buttonPanel.add(openButton);									 //
        buttonPanel.add(saveButton);									//
        															   //
        //Adds the buttons and the text area						  //
        add(buttonPanel, BorderLayout.PAGE_START);					 //
        add(log, BorderLayout.CENTER);								//
      //-----------------------------------------------------------//
    }
 
    public void actionPerformed(ActionEvent e)  //For the buttons
    {
        if (e.getSource() == openButton)  //open button
        {
            int returnVal = theChooser.showOpenDialog(FileChooser.this);
 
            if (returnVal == JFileChooser.APPROVE_OPTION)  //if you select something
                log.append("Opening: " + theChooser.getSelectedFile().getName() + ". (but not really) \n");
            else                                           //if you cancel
                log.append("Open command cancelled by user. (nothing would have happened anyway) \n");
        }
        else if (e.getSource() == saveButton)  //save button
        {
            int returnVal = theChooser.showSaveDialog(FileChooser.this);
            
            if (returnVal == JFileChooser.APPROVE_OPTION)  //if you select something
                log.append("Saving: " + theChooser.getSelectedFile().getName() + ". (but not really) \n");
            else                                           //if you cancel
                log.append("Save command cancelled by user. (nothing would have happened anyway) \n");
        }
        
        log.setCaretPosition(log.getDocument().getLength());
    }
 
    public static void main(String[] args)
    {
    	JFrame frame = new JFrame("FileChooserDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new FileChooser());
        frame.pack();
        frame.setVisible(true);
    }
}   