import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import java.io.*; /** * GUITail is a small application (GUI of-course) that * has a textfield for file input. It then prints out all * the contents of the file, and tails it (watches it grow). * NOTE: This written entirely with swing components. * It does not have threads, but does use inner classes. * * Questions, comments, etc, please email me. * @author Nick * (see http://illx.org for more details) */ public class GUItail { JTextArea jt = null; boolean Break = true; public JTextField tf; public JLabel errorLabel; public int c = 0; public String MyFile; public static void main(String[] args) { GUItail gt = new GUItail(); gt.go(); } public void go() { JFrame f = new JFrame(); f.setTitle("Nick's Java GUITail (http://illx.org)"); JPanel mp = new JPanel(); //main panel JPanel bp = new JPanel(); //bottom panel errorLabel = new JLabel(""); jt = new JTextArea(30,70); tf = new JTextField("enter filename here", 20); tf.addMouseListener(new MListener()); JButton start = new JButton("Start"); start.addActionListener(new GUItail.StartListener()); JButton stop = new JButton("Stop"); stop.addActionListener(new GUItail.StopListener()); bp.add(BorderLayout.NORTH, tf); bp.add(BorderLayout.WEST, start); bp.add(BorderLayout.EAST, stop); JScrollPane scroller = new JScrollPane(jt); scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED); scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); scroller.setAutoscrolls(true); mp.add(scroller); f.getContentPane().add(BorderLayout.NORTH, mp); f.getContentPane().add(BorderLayout.CENTER, errorLabel); f.getContentPane().add(BorderLayout.SOUTH, bp); f.setSize(800,550); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } //this classes whole existence is to clear the JTextField // when clicked on!! public class MListener implements MouseListener { public void mouseEntered(MouseEvent me) { } public void mouseExited(MouseEvent me) { } public void mouseReleased(MouseEvent me) { } public void mousePressed(MouseEvent me) { } public void mouseClicked(MouseEvent me) { if(c == 0) { tf.setText(""); c++; } } } public class StartListener implements ActionListener { public void actionPerformed(ActionEvent e) { jt.setText(""); Break = true; Thread t = new Thread(new Runnable() { public void run() { tail(); } }); t.start(); } } public class StopListener implements ActionListener { public void actionPerformed(ActionEvent e) { Break = false; jt.append("*** Stopped watching '"+MyFile+"' ***"); } } public void tail() { try { MyFile = tf.getText(); if(MyFile.equals("")) { errorLabel.setText("Error! You need to enter a filename"); } else { File f = new File(MyFile); FileReader fileReader = new FileReader(f); BufferedReader r = new BufferedReader(fileReader); String line = null; while(Break == true) { int c = r.read(); char aRc = (char)c; if (c != -1) { line = r.readLine(); jt.append(aRc+line+"\n"); } else { try {Thread.sleep(500);} catch (InterruptedException e) {} } } r.close(); } } catch(Exception e) { errorLabel.setText(e.getMessage()); } } }