FileDocCategorySizeDatePackage
TextPreview.javaAPI DocExample2045Sun Mar 28 19:08:44 BST 1999None

TextPreview

public class TextPreview extends JTextArea implements PropertyChangeListener

Fields Summary
File
selectedFile
String
preview
int
previewLength
Constructors Summary
public TextPreview(JFileChooser fc)

  
     
    super(10, 20);
    this.setEditable(false);
    this.setPreferredSize(new Dimension(150, 150));
    this.setLineWrap(true);
    fc.addPropertyChangeListener(this);
  
Methods Summary
voidloadText()


    if (selectedFile != null) {
      try {
        FileInputStream fin = new FileInputStream(selectedFile);
        byte[] data = new byte[previewLength];
        int bytesRead = 0;
        for (int i = 0; i < previewLength; i++) {
          int b = fin.read();
          if (b == -1) break;
          bytesRead++;
          data[i] = (byte) b;
        }
        preview = new String(data, 0, bytesRead);
        fin.close();
      }
      catch (IOException e) {
        // File preview is not an essential operation so 
        // we'll simply ignore the exception and return
      }
    }
    
  
public static voidmain(java.lang.String[] args)

    
    JFileChooser fc = new JFileChooser();
    fc.setAccessory(new TextPreview(fc));
    int result = fc.showOpenDialog(new JFrame());
    if (result == JFileChooser.APPROVE_OPTION) {
      try {
        File f = fc.getSelectedFile();
        if (f != null) {
          FileInputStream fin = new FileInputStream(f);
          StreamCopier.copy(fin, System.out);
          fin.close();
        }
      }
      catch (IOException e) {
        System.err.println(e);
      }
      
    }
    
    System.exit(0);

  
public voidpropertyChange(java.beans.PropertyChangeEvent e)

  
    if (e.getPropertyName().equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) {
      selectedFile = (File) e.getNewValue();
      if(isShowing()) {
        loadText();
        this.setText(preview);
      }
    }