Source of UniversalTextViewer.java


  1: 
  2: import java.awt.*;
  3: import java.awt.event.*;
  4: import java.io.*; 
  5: import javax.swing.*;
  6: 
  7: public class UniversalTextViewer extends JPanel { 
  8:   
  9:   public UniversalTextViewer(String filename, String enc) {
 10:     setLayout(new BorderLayout());
 11:     JTextArea textarea = new JTextArea(40, 80); 
 12:     textarea.setEditable(false); 
 13:     textarea.setFont(new Font("Monospaced", Font.BOLD, 16)); 
 14:     add("Center", new JScrollPane(textarea)); 
 15:     try {
 16:       BufferedReader in = 
 17:           new BufferedReader(
 18:           new InputStreamReader(new FileInputStream(filename), enc)); 
 19:       String line; 
 20:       while ((line = in.readLine()) != null) {
 21:         textarea.append(line + "\n"); 
 22:       }
 23:     } catch (IOException e) {}
 24:   }
 25: 
 26:   public static void main(String args[]) {
 27:     if (args.length >= 2) {
 28:       JFrame frame = new JFrame();
 29:       frame.setTitle("Universal Text Viewer: " + 
 30:                      args[0] + " [" + args[1] + "]");
 31:       frame.setBackground(Color.lightGray);
 32:       frame.getContentPane().setLayout(new BorderLayout());
 33:       frame.getContentPane().add("Center", new UniversalTextViewer(args[0], args[1]));
 34:       frame.addWindowListener(new AppCloser());
 35:       frame.setSize(400, 300);
 36:       frame.show();
 37:     }
 38:   }
 39: 
 40:   protected static final class AppCloser extends WindowAdapter {
 41:     public void windowClosing(WindowEvent e) {
 42:       System.exit(0);
 43:     }
 44:   }
 45:   
 46: }