How create a console using java graphical interface

B

behnaz

Hi,

I am wondering if there is any simple way for creating a console that
outputs an application
results using java graphical interface.I don't wanna use netbeans or
a stuff like that,just a
standard way.
help me please......
B.
 
A

Andrew Thompson

behnaz wrote:
...
I am wondering if there is any simple way for creating a console that
outputs an application
results using java graphical interface.

I would use JTextArea.append()*, as opposed to
System.out.println() for those situations, but I
am not sure I understand your question.

* ..but on an *instance* of a JTextArea, of course.
..I don't wanna use netbeans or
a stuff like that,just a
standard way.

It might be desirable for a development tool such as
an IDE to redirect System.out to a GUI element,
but that is an extraordinary situation, and it could
not therefore be defined as usual, or standard.


Now a couple of tips on communicating a message.

To help communicate a message, it pays to use
the standard spacing people expect to see. I have
used it in my reply to you. It means using '2 spaces'
after each full-stop (.), and one space immediately
after a comma (,).
help me please......

Your last sentence also leads me to point out that
every sentence should begin with a single Upper
Case letter. Good work on making the 'I' upper case,
by the way, most folks get that wrong.

Last tip is to not add things like 'help me please'
at all. They make a post sound 'needy', and people
are less likely to help.

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200709/1
 
M

Mark Space

behnaz said:
Hi,

I am wondering if there is any simple way for creating a console that
outputs an application
results using java graphical interface.I don't wanna use netbeans or
a stuff like that,just a
standard way.
help me please......
B.


java.lang.System.setOut() and java.lang.System.setErr() will allow you
to change standard output and standard error streams to something else,
like the afore mentioned JTextArea. (Not automagically, you'll have to
write some code to get it all to work properly.)

You CAN still also just write to .out and .err. Even though your
application uses a GUI, the console streams are still there. To use
them, open a command line window and run your app (or any app) from the
command line. You'll see the output as the program runs.

Finally, I think the best way to actually capture print-like streams is
to use the Logger.

http://java.sun.com/javase/6/docs/api/java/util/logging/Logger.html

Lot's of info and tutorials available for the latter, I won't duplicated
them here. GIYF.
 
S

Stefan Ram

Mark Space said:
to change standard output and standard error streams to something else,
like the afore mentioned JTextArea. (Not automagically, you'll have to
write some code to get it all to work properly.)

I use a JTextArea within a JScrollPane for output and wrote
some code to make it scroll to the end, when new text is
written to it - there was no simple call for this.

Now I saw that someone uses a JEditorPane for the same
purpose. This seems to be smarter because JEditorPane is
operating on a higher lever and might already know how to
scroll to the end (for the JTextArea, I need to find the
offset of the last line myself and then scrollRectToVisible).
Possibly saving the output to a file might also be simpler.

So, to print output or log information, what would be the
»better« console replacement: (an extension of )JTextArea or
JEditorPane?
 
R

Roedy Green

I am wondering if there is any simple way for creating a console that
outputs an application
results using java graphical interface.I don't wanna use netbeans or
a stuff like that,just a
standard way.
help me please......

Here is one that provides a scrollable table to display the log.

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 */
}

}

}
 
S

steph

behnaz said:
Hi,

I am wondering if there is any simple way for creating a console that
outputs an application
results using java graphical interface.I don't wanna use netbeans or
a stuff like that,just a
standard way.
help me please......
B.

What would be the added of such tool compare to a windows cmd or an unix
terminal ?
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Mark said:
java.lang.System.setOut() and java.lang.System.setErr() will allow you
to change standard output and standard error streams to something else,
like the afore mentioned JTextArea. (Not automagically, you'll have to
write some code to get it all to work properly.)

And if the original poster want an example then I had something
on the shelf.

Arne

============================================

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

public class CaptureOutput {
public static void main(String[] args) {
try {
PrintStream save = System.out;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream pw = new PrintStream(baos);
System.setOut(pw);
T t = new T();
t.start();
t.join();
System.setOut(save);
pw.flush();
System.out.println("from thread: " + baos.toString());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

class T extends Thread {
public void run() {
System.out.println("Hello world");
}
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top