import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class FileFinderSample extends JFrame implements ActionListener
{
private static final int FRAME_WIDTH = 320;
private static final int FRAME_HEIGHT = 225;
private static final int FRAME_X_ORIGIN = 100;
private static final int FRAME_Y_ORIGIN = 150;
private static final int BUTTON_WIDTH = 80;
private static final int BUTTON_HEIGHT = 30;
private JButton browseButton;
private String dirName;
private String fileName;
public FileFinderSample( )
{
Container contentPane = getContentPane();
setTitle ("File Finder Sample");
setSize (FRAME_WIDTH, FRAME_HEIGHT);
setLocation (FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
setResizable(false);
contentPane.setLayout(null);
contentPane.setBackground(Color.white);
browseButton = new JButton("Browse");
browseButton.setBounds(125, 80, BUTTON_WIDTH, BUTTON_HEIGHT);
contentPane.add(browseButton);
browseButton.addActionListener(this);
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent event)
{
JButton clickedButton = (JButton) event.getSource();
String buttonText = clickedButton.getText();
if(clickedButton == browseButton)
{
JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(this);
File selectedFile = null;
String directory = "";
String filename = "";
if(returnVal == JFileChooser.APPROVE_OPTION)
{
selectedFile = fc.getSelectedFile();
File dir = fc.getCurrentDirectory();
directory = dir.getAbsolutePath();
filename = selectedFile.getAbsolutePath();
}
dirName = directory;
fileName = filename;
System.out.println("Filename: " + fileName);
}
}
public static void main(String[] args)
{
FileFinderSample menu = new FileFinderSample();
}
}