printing groovy console on a Editor Pane
I need to print the contents of my groovy console onto a swing Builder's Editor pane/textarea. How can I do it?
Is there any开发者_开发百科 reusable code/class to achieve this?
Your solution is going to depend on how you instantiate the groovyConsole and how and why you want to access it.
When you say contents, do you mean the output of running a script from the console or the script displayed in the editor area of the console itself?
Note that the groovy console is itself using a swing builder instantiated set of textareas and editor panes. See the source code at: http://svn.codehaus.org/groovy/trunk/groovy/groovy-core/src/main/groovy/ui/Console.groovy
See the main method for how the console is created. If you do that in your own code and keep a reference to the console object you should be able to access the contents of the various text areas.
Thanks!! A simple redirect of println onto a new printstream will do. Here is the code. This class will create an internal frame that will display a text area holding all the sysout and syserr statements.
public class ConsoleFrame extends JInternalFrame
{
JTextArea outArea = new JTextArea(300,300);
static JInternalFrame cons;
public ConsoleFrame()
{
JScrollPane pain = new JScrollPane(outArea);
//pain.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
this.add(pain);
this.setVisible(true);
this.setSize(785,255);
this.setTitle("Groovy Console");
this.closable = false;
this.maximizable = false;
this.isSelected = true;
this.resizable = false;
BasicInternalFrameUI ui = (BasicInternalFrameUI)this.getUI();
Component north = ui.getNorthPane();
MouseMotionListener[] actions =
(MouseMotionListener[])north.getListeners(MouseMotionListener.class);
for (int i = 0; i < actions.length; i++)
north.removeMouseMotionListener( actions[i] );
this.setFocusable(false);
System.setOut(new PrintStream(new JTextAreaOutputStream(outArea)));
System.setErr(new PrintStream(new JTextAreaOutputStream(outArea)));
setConsole(this);
}
static public JInternalFrame getConsole(){
return cons;
}
public void setConsole(JInternalFrame console){
cons = console;
}
public class JTextAreaOutputStream extends OutputStream {
JTextArea ta;
public JTextAreaOutputStream(JTextArea t) {
super();
ta = t;
}
public void write(int i) {
ta.append(Character.toString((char)i));
}
public void write(char[] buf, int off, int len) {
String s = new String(buf, off, len);
ta.append(s);
}
}
}
精彩评论