Uses a FileDialog to choose the file to display #Programming Code Examples #Java/J2EE/J2ME #AWT Components

DisplayFile.java 
****************
import java.awt.*;
import java.awt.event.*;
import java.io.*;

/** Uses a FileDialog to choose the file to display. 
 ***************
 
public class DisplayFile extends CloseableFrame 
                         implements ActionListener {
                                
  public static void main(String[] args) {
    new DisplayFile();
  }

  private Button loadButton;
  private TextArea fileArea;
  private FileDialog loader;

  public DisplayFile() {
    super("Using FileDialog");
    loadButton = new Button("Display File");
    loadButton.addActionListener(this);
    Panel buttonPanel = new Panel();
    buttonPanel.add(loadButton);
    add(buttonPanel, BorderLayout.SOUTH);
    fileArea = new TextArea();
    add("Center", fileArea);
    loader = new FileDialog(this, "Browse", FileDialog.LOAD);
    // Default file extension: .java.
    loader.setFile("*.java");
    setSize(350, 450);
    setVisible(true);
  }

  /** When the button is clicked, a file dialog is opened. When 
   * the file dialog is closed, load the file it referenced.
   */
  
  public void actionPerformed(ActionEvent event) {
      loader.show();
      displayFile(loader.getFile());
  }

  public void displayFile(String filename) {
    try {
      File file = new File(filename);
      FileInputStream in = new FileInputStream(file);
      int fileLength = (int)file.length();
      byte[] fileContents = new byte[fileLength];
      in.read(fileContents);
      String fileContentsString = new String(fileContents);
      fileArea.setText(fileContentsString);
    } catch(IOException ioe) {
      fileArea.setText("IOError: " + ioe);
    }
  }
}
*************
CloseableFrame.java. 
*************
import java.awt.*;
import java.awt.event.*;

/** A Frame that you can actually quit. Used as the starting 
 *  point for most Java 1.1 graphical applications.
 **********************

public class CloseableFrame extends Frame {
  public CloseableFrame(String title) {
    super(title);
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
  }

  /** Since we are doing something permanent, we need
   *  to call super.processWindowEvent first.
   */
  
  public void processWindowEvent(WindowEvent event) {
    super.processWindowEvent(event); // Handle listeners.
    if (event.getID() == WindowEvent.WINDOW_CLOSING) {
      // If the frame is used in an applet, use dispose().
      System.exit(0);
    }
  }
}

Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017 From: http://sitestree.com/?p=10331
Categories:Programming Code Examples, Java/J2EE/J2ME, AWT Components
Tags:Java/J2EE/J2MEAWT Components
Post Data:2017-01-02 16:04:35

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada