flush the content of a TextArea?

Discussion in 'Java' started by wang, Oct 21, 2005.

  1. wang

    wang Guest

    Hi all,
    I'm again here with a problem:
    I use TextArea to report to the user what the program
    is just doing. But it seems to me that, if the program
    takes quite a while to run, the report messages appear
    only after the program has finished the operations. So
    the messages have told the user what the program HAS DONE
    chronologically. This is not what I want. I've searched
    in the API document for a methode as flush(), but have
    found nothing. How can the content of a TextArea be
    flushed? Or is there better, more suitable way to do
    the task than TextArea?
    Many thanks in advance!

    k.w.wang
     
    wang, Oct 21, 2005
    #1
    1. Advertising

  2. wang

    wang Guest

    Excuse me. I mean JTextArea in swing
    instead of TextArea in awt.
    k.w.wang
     
    wang, Oct 21, 2005
    #2
    1. Advertising

  3. wang wrote:

    > I'm again here with a problem:
    > I use TextArea


    <http://www.physci.org/codes/javafaq.jsp#cljg>
    ...is best for those questions.

    >..to report to the user what the program
    > is just doing. But it seems to me that, if the program
    > takes quite a while to run, the report messages appear
    > only after the program has finished the operations. So
    > the messages have told the user what the program HAS DONE
    > chronologically. This is not what I want. I've searched
    > in the API document for a methode as flush(),


    Don't "block the EDT". Refer to the GUI FAQ for further details.

    HTH
     
    Andrew Thompson, Oct 21, 2005
    #3
  4. wang

    Roedy Green Guest

    On 21 Oct 2005 03:02:57 -0700, "wang" <> wrote or
    quoted :

    >I'm again here with a problem:
    >I use TextArea to report to the user what the program
    >is just doing. But it seems to me that, if the program
    >takes quite a while to run, the report messages appear
    >only after the program has finished the operations. So
    >the messages have told the user what the program HAS DONE
    >chronologically. This is not what I want. I've searched
    >in the API document for a methode as flush(), but have
    >found nothing. How can the content of a TextArea be
    >flushed? Or is there better, more suitable way to do
    >the task than TextArea?
    >Many thanks in advance!


    TextAreas and JTextAreas display any new data within a fraction of a
    second unless you have screwed up something with your threads. see
    http://mindprod.com/jgloss/swingthreads.html

    If want a simple logging class that scrolls onscreen you might use
    something like this:

    package xxxx;

    import java.awt.BorderLayout;
    import java.awt.Dimension;

    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.TableColumn;
    import javax.swing.table.TableColumnModel;

    /*
    * Logs both to a Swing console and the DOS console.
    * Since we are in an Applet, we cannot also log to a file.
    */

    public class Log
    {

    /**
    * severity of message
    */
    public final static int INFO = 0;
    public final static int WARNING = 1;
    public final static int ERROR = 2;
    public final static int FATAL = 3;
    public final static int BUG = 4;

    /**
    * JFrame, but do not allow close
    */
    private static JFrame console;

    /**
    * data for the log. Drops off data after a while.
    */
    private static LogTableModel tableModel;
    /**
    * GUI visible console log
    */
    private static JTable jtable;

    /**
    * Open the log.
    */
    public static void open()
    {
    console = new JFrame("Console Log");
    /* make it so the user can't close the Frame */
    console.setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE );
    tableModel = new LogTableModel();

    jtable = new JTable( tableModel );
    jtable.setBackground( Config.LOG_BACKGROUND );
    jtable.setForeground( Config.LOG_FOREGROUND );
    TimestampRenderer.install( jtable, 0 );
    SeverityRenderer.install( jtable, 1 );
    // pad the message column out a bit
    TableColumnModel tcm = jtable.getColumnModel();
    TableColumn tc = tcm.getColumn( 2 );
    tc.setPreferredWidth( 300 );

    jtable.setPreferredScrollableViewportSize( new Dimension(300,
    300) );

    //Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(jtable);

    //Add the scroll pane to this window.
    console.getContentPane().add( scrollPane, BorderLayout.CENTER );

    console.pack();
    console.setLocation( 300, 300 );
    console.setVisible( true );
    if ( false )
    {
    // sample test
    println ( ERROR, "dummy test " );
    println ( WARNING , "a much much bigger test
    abcdefghijklmnopqrstuvwxyz " );
    println ( INFO, "dummy info" );
    println ( FATAL, "if the world were ending");
    println ( BUG, "test of bug shower.");
    }
    }
    /**
    * close the log.
    */
    public static void close ()
    {
    console.dispose();
    console = null;
    tableModel = null;
    jtable = null;
    }

    /**
    * log the string
    *
    * @param severity Severity of error: INFORMATIONAL, WARNING,
    * ERROR, FATAL, BUG
    *
    * @param s string to log
    *
    * @exception IOException
    */
    public static void println( int severity, String s )
    {

    tableModel.addRow ( severity, s );
    String level;
    switch ( severity )
    {

    case INFO:
    level = "info: ";
    break;

    case WARNING:
    level = "warning: ";
    break;

    case ERROR:
    level = "error: ";
    break;

    case FATAL:
    level = "FATAL: ";
    break;

    default:
    case BUG:
    level = "BUG: ";
    break;
    }
    System.out.println ( level + s );

    }

    /**
    * log the INFO string both to the Swing and DOS console.
    *
    * @param s string to log.
    *
    * @exception IOException
    */
    public static void println( String s )
    {
    println ( INFO, s );
    }

    } // end class Log

    package xxxx;

    import java.util.Date;
    import java.util.Vector;

    import javax.swing.table.AbstractTableModel;
    import javax.swing.table.TableModel;

    /**
    * TableModel for a streaming Activity Table.
    */
    public class LogTableModel extends AbstractTableModel implements
    TableModel
    {

    /**
    * Each element of the Vector is one row.
    * Data are Date, Integer(severity), String Messaage.
    * Vector because we fill the model from various threads.
    */

    private Vector data = new Vector( Config.LOG_SIZE );
    /**
    * number of columns in the table
    */
    private final int numCols = 3;
    /**
    * column titles
    */
    private String[] columnNames = {"Time", "Severity", "Message"};

    /**
    * constructor
    * Just saves column names, does not fetch any data.
    *
    * @param columnNames column titles, 0-based
    * @param binarizer Contains data types of the various columns
    */
    public LogTableModel( )
    {
    // just went from 0 to 3 cols, headers now defined,
    fireTableStructureChanged();
    }

    /**
    * Returns the number of row in the model. A
    * <code>JTable</code> uses this method to determine how many
    columns it
    * should create and display by default.
    *
    * @return the number of rows in the model
    */
    public int getRowCount()
    {
    return data.size();
    }
    /**
    * Returns the number of columns in the model. A
    * <code>JTable</code> uses this method to determine how many
    columns it
    * should create and display by default.
    *
    * @return the number of columns in the model
    */
    public int getColumnCount()
    {
    return numCols;
    }

    /**
    * get title of for column.
    *
    * @param col zero-based column number
    *
    * @return String, no embedded \n
    */
    public String getColumnName(int col)
    {
    return columnNames[col];
    }
    /**
    * get value at point in table grid
    *
    * @param row zero-based row number
    * @param col zero-based column number
    * @return Object, will be String, Integer or Float.
    */
    public Object getValueAt(int row, int col)
    {
    try
    {

    Object[] rowData = (Object[]) data.elementAt(row);
    return rowData[col];
    }
    catch ( ArrayIndexOutOfBoundsException e )
    {
    // can happen if we shrink table in one thread
    // right after Swing gets size in another.
    // This element will disappear entirely to Swing
    // on the next look.
    return null;
    }

    }
    /**
    * set value at point in table grid.
    *
    * @param value will be String, Integer or Float ...
    * @param row zero-based row number
    * @param col zero-based column number
    */
    public void setValueAt(Object value, int row, int col)
    {
    throw new IllegalArgumentException("LogTableModel:setValueAt not
    implemented");
    }

    /**
    * No items are editable.
    *
    * @param row zero-based row number
    * @param col zero-based column number
    * @return false, to indicate no edits are possible.
    */
    public boolean isCellEditable(int row, int col)
    {
    return false;
    }

    /**
    * insert a new row, sliding existing rows
    * out the way. Does no duplicate avoidance processing or sorting.
    *
    * @param rowData row of Object data to add.
    * @param quietly true if should not do any FiretableRowChanged
    */
    public synchronized void addRow( int severity , String message )
    {
    if ( data.capacity() == data.size() )
    {
    data.removeElementAt( 0 );
    fireTableRowsDeleted( 0, 0 );
    }
    data.add( new Object[] { new Date(), new Integer( severity ),
    message} );
    int row = data.size()-1;
    fireTableRowsInserted( row, row );
    }

    /**
    * Returns <code>class</code> of column.
    *
    * @param columnIndex the column being queried
    * @return the class
    */
    public Class getColumnClass( int columnIndex )
    {
    switch ( columnIndex )
    {
    case 0: return Date.class; /* timestamp */
    case 1: return Integer.class; /* severity */
    default:
    case 2: return String.class; /* message */
    }

    }

    } // end class




    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Oct 22, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MRAB
    Replies:
    5
    Views:
    265
  2. Augustus
    Replies:
    1
    Views:
    383
    Ken Schaefer
    Sep 10, 2003
  3. Replies:
    0
    Views:
    234
  4. Textarea Inside of a textarea

    , Feb 4, 2006, in forum: ASP General
    Replies:
    6
    Views:
    348
    Anthony Jones
    Feb 5, 2006
  5. Replies:
    1
    Views:
    342
    Bart Van der Donck
    Jul 4, 2007
Loading...

Share This Page