import javax.swing.*;

import java.io.*;

/* Example program to read the first line of a file. */

public class IOexample extends JApplet{

      JLabel titleLabel;                        // GUI display field              

      JLabel showLine;                          // file data display field

 

public void init() {

      getContentPane().setLayout(null);         // use absolute positioning

      setSize(400, 100);                        // set size of applet

      titleLabel = new javax.swing.JLabel("Select a File");

      titleLabel.setBounds(10, 5, 380, 15);     // set position and size

      titleLabel.setHorizontalAlignment(javax.swing.JLabel.CENTER);

      getContentPane().add(titleLabel);         // put label in applet

      showLine = new JLabel();                  // create field to display data

      showLine.setBounds(10, 40, 380, 15);      // set position and size

      getContentPane().add(showLine);           // put label in applet

}

 

public void start() {

      JFileChooser chooser = new JFileChooser();

      FilterFree filter = new FilterFree();

      chooser.setFileFilter(filter);

      int returnVal = chooser.showOpenDialog(this);

      if(returnVal == JFileChooser.APPROVE_OPTION) {

            titleLabel.setText("You selected: "

                              + chooser.getSelectedFile().getName());

      }

      /* read the first line from the file */

      try {

            FileInputStream inStream =

                        new FileInputStream(chooser.getSelectedFile());

            InputStreamReader inReader = new InputStreamReader(inStream);

            BufferedReader inBuf = new BufferedReader(inReader);

            String firstLine = inBuf.readLine();      // read a line from the file

            showLine.setText(firstLine);              // display data in GUI

            inBuf.close();

      } catch (Throwable e) {

            e.printStackTrace();

      }

}

public static void main(String args[]) {

      try {

            javax.swing.UIManager.setLookAndFeel(

                  "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

      } catch (Throwable e) {

            System.out.println("Just don't have that look and feel");

      }

      javax.swing.JFrame myFrame = new javax.swing.JFrame("Frame Title");

      IOexample theAp = new IOexample();        // make applet object

      theAp.init();                             // initialize applet

      myFrame.setSize(theAp.getSize());         // frame size = applet size

      myFrame.getContentPane().add( theAp );    // put applet in frame

      myFrame.setVisible( true );               // show frame

      theAp.start();

}

}

 

/**

 * Filter to accept all files.

 */

public class FilterFree extends javax.swing.filechooser.FileFilter {

/**

 * This method is called by the JFileChooser to determine what files to

 * to display.

 */

public boolean accept(java.io.File f) {

      return true;

}

/**

 * Tell what files will be displayed.

 */

public String getDescription() {

      return "all files";

}

}