Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > flush the content of a TextArea?

Reply
Thread Tools

flush the content of a TextArea?

 
 
wang
Guest
Posts: n/a
 
      10-21-2005
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

 
Reply With Quote
 
 
 
 
wang
Guest
Posts: n/a
 
      10-21-2005
Excuse me. I mean JTextArea in swing
instead of TextArea in awt.
k.w.wang

 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      10-21-2005
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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-22-2005
On 21 Oct 2005 03:02:57 -0700, "wang" <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Help: Guide needed in trying to delete/flush the content of afifo file. MRAB Python 5 11-17-2010 07:29 PM
error: Only Content controls are allowed directly in a content page that contains Content controls. hazz ASP .Net 6 06-09-2010 01:54 PM
Response.flush not displaying content jose.mendez22@gmail.com ASP General 0 12-08-2005 03:56 PM
Re: Suggestion for Cache flush in Moz Caffeine Junkie Firefox 1 01-20-2004 06:06 AM
Re: Response.Flush() in code-behind Marina ASP .Net 1 07-01-2003 06:44 PM



Advertisments