We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpIntegration › minimal scrolling text field for output
Page Index Toggle Pages: 1
minimal scrolling text field for output (Read 1317 times)
minimal scrolling text field for output
Jul 22nd, 2006, 4:41am
 
I'd like simple replacements for print() and println() that send their text not only to the pde console but also to a scrolling text field that will appear in exported applets. I don't much care what the text field looks like, although I do want a scroll bar so that I can send an arbitrary amount of text. I worked up a crude graphics-based substitute -- building on the typography examples that come with Processing -- but I really want selectable and scrollable text instead.

I am finding this unexpectedly difficult to achieve. I'm new to Processing and not much of a Java programmer (although I have a lot of experience in other languages), so maybe I'm missing something obvious. But I've tried to consult the relvant parts of the core Processing docs, the library docs (e.g. for MyGui), the AWT docs, and the forums, and although I've had many near misses I can't quite do what I need to do, though it seems like it should be simple.

The best I've been able to achieve is the code presented below, for two approaches: one using an AWT textArea and one using a MyGUITextInput object. Both have serious pathologies. The AWT approach almost works but it's strangely flakey -- sometimes it works and sometimes it doesn't. When it doesn't it displays only an empty textArea. I can run the thing several times in a row, either in the pde or as an applet in Safari, and I get a seemingly random sequence of working and not working. If I could eliminate this flakeyness then I'd be perfectly happy with the AWT solution. (In case it matters I'm running this under Mac OS 10.4.6 on a G4 Powerbook.)

The MyGUITextInput approach doesn't provide a scroll bar, and excess text actually spills out of the MyGUITextInput object and onto the background -- very odd looking. I'm also not crazy about this approach because it requires installation of a library and I'll be using this at the start of a course for beginning programmers -- I'd like students to be able to get it up and running with a standard install of Processing if possible. Of course the library installation isn't that big a deal and we could live with it, but if there's a way to do this without installing a library I'd prefer it.

I'd appreciate any help anyone can provide either on debugging the approaches presented here or on new/better approaches.

Thanks for any help you can provide! -Lee

// the AWT approach
import java.awt.TextArea;

TextArea screen_text;

void setup () {
 size(600, 400);
 background(255); // white
 noLoop();
 screen_text = new TextArea("",7,31,1);
 this.add(screen_text);
}

// print both to the pde and to the field on the screen
void textln(String text_to_print) {
 println(text_to_print);
 screen_text.setText(screen_text.getText() + text_to_print + "\n");
 // screen_text.append(text_to_print);  // should this work instead?
}

void draw () {
 for(int i=0; i<40; i=i+1) {
   textln("the rain in spain");
 }
}


// the MyGUITextInput approach
import mkv.MyGUI.*;

MyGUI gui;
MyGUITextInput screen_text;

void setup () {
 size(600, 400);
 background(255); // white
 noLoop();
 gui = new MyGUI(this);
 screen_text = new MyGUITextInput(this, width/2, height/2, 300, 300);;
 gui.add(screen_text);
}

// print both to the pde and to the field on the screen
void textln(String text_to_print) {
 println(text_to_print);
 screen_text.setValue(screen_text.getValue() + text_to_print + "\n");
}

void draw () {
 for(int i=0; i<40; i=i+1) {
   textln("the rain in spain");
 }
}


--
Lee Spector, Professor of Computer Science
School of Cognitive Science, Hampshire College
893 West Street, Amherst, MA 01002-3359
lspector@hampshire.edu, http://hampshire.edu/lspector/
Phone: 413-559-5352, Fax: 413-559-5438
Re: minimal scrolling text field for output
Reply #1 - Jul 22nd, 2006, 2:06pm
 
Take a look at fry's answer the end of this post:
http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Integrate;action=display;num=1143760874
Re: minimal scrolling text field for output
Reply #2 - Jul 22nd, 2006, 5:40pm
 
eskimoblood: Thanks for the pointer, but it hasn't yet led me to an answer... Is the idea that I have to make an applet within the applet to hold the textArea? That would seem like overkill and I'm not sure how to do it... Following up fry's mentions of noLoop() and redraw(): I'm already calling noLoop() although I now see that it's supposed to be the last thing in setup() -- but I changed that and I still have the same flakiness (no text sometimes, and sometimes not even any textArea frame). I don't see where it would make sense to add a redraw() but I tried a couple of places anyway, to no avail.

Maybe I'm not seeing what you intended with this reference?

Re: minimal scrolling text field for output
Reply #3 - Jul 22nd, 2006, 7:15pm
 
If you search the forum for awt and textarea you will see that there is no way to add the textarea in your way. So you have to add the PApplet as an component to an applet or application with a swing layout.
Re: minimal scrolling text field for output
Reply #4 - Jul 22nd, 2006, 10:49pm
 
Are there any minimal examples of how to "add the PApplet as an component to an applet or application with a swing layout"?

I don't know much about PApplets or swing or layouts, there seems to be little in the Processing docs about this (or I'm missing it), and I haven't been able to discover much from searches for combinations of {PApplet, swing, example, ...} in the Processing site. I see some related discussion but no examples or full explanations.

Any pointers would be appreciated, either to example code or to other approaches.

Re: minimal scrolling text field for output
Reply #5 - Aug 30th, 2006, 5:10am
 
FYI I think we've resolved this, thanks to the work of Gabriel Tarasuk-Levin. He found that the AWT approach seems to work reliably if the call to size() occurs after the text box is added. I believe this contradicts advice in the Processing docs that says that size() should be the first thing in setup(), but it does appear to work.

I also discovered that even the OLD AWT approach (the code in the first post on this thread) works reliably on my new MacBook Pro, which must have come with new java support files... So maybe the problem was never in Processing per se. I don't know for sure.

In any event we now appear to have a solution (assuming this works across platforms). Here's a version of the code that works:

import java.awt.TextArea;

TextArea screen_text;

void setup () {
 background(255); // white
 screen_text = new TextArea("",23,45,1);
 this.add(screen_text);
 size(500, 400); // supposed to be first in setup(), but call after adding text area
 noLoop(); // must be last thing in setup()
}

// print both to the pde and to the field on the screen
void textln(String text_to_print) {
 println(text_to_print);
 screen_text.append(text_to_print + "\n");
}

void draw () {
 for(int i=0; i<40; i=i+1) {
   textln("the rain in spain");
 }
}

Page Index Toggle Pages: 1