Real Time Updating from a text file

From: John Blaze (tical2000_at_rogers.com)
Date: 03/29/04


Date: 29 Mar 2004 07:24:23 -0800

Hello,

I have a requirement to read from a text file into a GUI. Basically I
would like to have the equivalent of a 'tail -f file' running in the
top portion of the GUI. I have included the code that I am using,
which is a kludgey modification of the ListSelectionDemo.java example.
 Any help with this would be greatly appreciated. Also, when I click
on an item in the top list I receive double output (one for mouse-down
and one for mouse-up) how do I separate this out?

Thanks in Advance,
John

//////// Code Start ///////////////
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

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

public class ListSelectionDemo extends JPanel {
        JTextArea output;
    JList list;
    JTable table;
    String newline = "\n";
    ListSelectionModel listSelectionModel;
    
    StringThing st = new StringThing();

    public ListSelectionDemo() {
            super(new BorderLayout());
        try {
                list = new JList(st.makeList());
                 }
                 catch (IOException ex) {
                          System.exit(0);
                  }
        
        listSelectionModel = list.getSelectionModel();
        listSelectionModel.addListSelectionListener(
        new SharedListSelectionHandler());
        JScrollPane listPane = new JScrollPane(list);

        JScrollPane tablePane = new JScrollPane(table);

        //Build control area (use default FlowLayout).
        JPanel controlPane = new JPanel();

        //Build output area.
        output = new JTextArea(1, 10);
        output.setEditable(false);
        JScrollPane outputPane = new JScrollPane(output,
                        
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                        
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        //Do the layout.
        JSplitPane splitPane = new
JSplitPane(JSplitPane.VERTICAL_SPLIT);
        add(splitPane, BorderLayout.CENTER);

        JPanel topHalf = new JPanel();
        topHalf.setLayout(new BoxLayout(topHalf,
BoxLayout.LINE_AXIS));
        JPanel listContainer = new JPanel(new GridLayout(1,1));
        listContainer.setBorder(BorderFactory.createTitledBorder(
                                                "List"));
        listContainer.add(listPane);
        JPanel tableContainer = new JPanel(new GridLayout(1,1));

        topHalf.setBorder(BorderFactory.createEmptyBorder(5,5,0,5));
        topHalf.add(listContainer);
        topHalf.add(tableContainer);

        topHalf.setMinimumSize(new Dimension(400, 50));
        topHalf.setPreferredSize(new Dimension(450, 300));
        splitPane.add(topHalf);

        JPanel bottomHalf = new JPanel(new BorderLayout());
        bottomHalf.add(controlPane, BorderLayout.PAGE_START);
        bottomHalf.add(outputPane, BorderLayout.CENTER);
        //XXX: next line needed if bottomHalf is a scroll pane:
        //bottomHalf.setMinimumSize(new Dimension(400, 50));
        bottomHalf.setPreferredSize(new Dimension(450, 135));
        splitPane.add(bottomHalf);
        
    }
    
    
    /**
     * Create the GUI and show it. For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Make sure we have nice window decorations.
        JFrame.setDefaultLookAndFeelDecorated(false);
        

        //Create and set up the window.
        
        JFrame frame = new JFrame("ListSelectionDemo");
        frame.setExtendedState(Frame.MAXIMIZED_BOTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        //frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

        //Create and set up the content pane.
        ListSelectionDemo demo = new ListSelectionDemo();
        demo.setOpaque(true);
        frame.setContentPane(demo);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

   class SharedListSelectionHandler implements ListSelectionListener {
        public void valueChanged(ListSelectionEvent e) {
            ListSelectionModel lsm =
(ListSelectionModel)e.getSource();

            int firstIndex = e.getFirstIndex();
            int lastIndex = e.getLastIndex();
            String selectedItem = e.toString();
            boolean isAdjusting = e.getValueIsAdjusting();
            StringThing st4 = new StringThing();
            
            try {
                           String[] str4 = st.makeList();
                     String selection =
list.getModel().getElementAt(list.getSelectedIndex()).toString();
                     output.append(selection);
                         }
                         catch (IOException ex) {
                                  System.exit(0);
                          }
            output.append(newline);
        }
    }
    
        class StringThing {

                public String[] makeList() throws IOException {
                          File inputFile = new File("/home/sscrn1/data/datafile");
                    File inputFile2 = new File("/home/sscrn1/data/datafile");
                     FileReader in = new FileReader(inputFile);
                FileReader in2 = new FileReader(inputFile2);
            
                      LineNumberReader lnr = new LineNumberReader(in) ;
                LineNumberReader lnr2 = new LineNumberReader(in2) ;
                      String str;
                String str2;
            
                      while ((str = lnr.readLine()) != null) {
                              //System.out.println(str);
                      }
                
                      int getLine = lnr.getLineNumber();
                      in.close();
                
                      String strLines[] = new String[getLine];
                
                int i = 0;
                
                while ((str2 = lnr2.readLine()) != null) {
                        strLines[i] = str2;
                        i++;
                        //System.out.println(i);
                }
                
                in2.close();
            
                return strLines;
                   }
               
                private String getWord(FileReader fr) throws IOException {
            
                        StringBuffer s = new StringBuffer();
                        for(int readChar = fr.read(); (char)readChar != ',' && readChar !=
-1 && (char)readChar != ' '; readChar = fr.read()) {
                                s.append((char)readChar);
                        }
                        return s.toString();
            }
        }
}
    
///////// Code End //////////////////////////