append a char to a JTextArea

So I'm working on making a class that will "type" letters into a JTextArea, pausing between each letter to make it look like someone is typing. The only way I can come up with is using output.append(char) in a loop with a pause. The only problem... you can only append Strings to JTextAreas.

So is there any way to convert a char to a string so that I can append it letter by letter???

Here is my code so far:

import objectdraw.*; import java.awt.*; import javax.swing.*; public class TypeWriter extends ActiveObject implements Drawable { private char [] letter; private JTextArea cp; public TypeWriter(String sentence, JTextArea console) { /* break string into characters and save the console * for later use */ letter = new char[sentence.length()]; cp = console; for(int i=0; i<sentence.length(); i++) { letter[i] = sentence.charAt(i); } start(); } public void run() { // append each letter, pausing between them for( char s : letter ) { cp.append(new String(s)); pause(50); } } /* More methods for the * Drawable interface... */ }

-------------Problems Reply------------

Don't use characters.

String foo = "This is a String";
for (int i = 0; i < foo.length(); i++)
String subString = foo.substring(i, i+1);

Use the String(char[] value) constructor so

String s = "";
s += Character.toString('s');

Category:java Views:0 Time:2011-11-09

Related post

  • How to append a char to a std::string? 2009-09-24

    The following fails with the error prog.cpp:5:13: error: invalid conversion from ‘char’ to ‘const char*’ int main() { char d = 'd'; std::string y("Hello worl"); y.append(d); // Line 5 - this fails std::cout << y; return 0; } I also tried, the f

  • Segmentation fault when appending two chars - C++ 2011-02-13

    I am trying to append two chars but for some reason I am getting a segmentation fault. My code is like; #include <string.h> char *one = (char*)("one"); char *two = (char*)("two"); strcat(one, two); and I seem to be getting a segmentation fault

  • Appending a text to a JTextArea after Using a DocumentFilter 2011-09-20

    I am a having problem with appending a text to a JTextArea after Using a DocumentFilter, I need to append a string on the JTextArea after the text has been uploaded from a file and also return a string from a JTextArea of another JFrame to the specif

  • Appending a char to string 2011-10-21

    I'm trying to append a char to string. I tried char *string = malloc(strlen(text) * sizeof (char)); for(i=0, i <n; i++) { j = i; while (j <= strlen(text)) { string[strlen(string)] = text[i]; j = j + n; } string[strlen(string)] = ''; printf("%s"

  • Appending text in Java's JTextArea 2011-01-31

    I have a problem with my text area. I use jTextArea1.append("cleverly amusing"); to add the text.. FIRST APPEND: then I use jTextArea1.append("a fight"); to add the next text. SECOND APPEND What I Really want is to replace the "cleverly amusing" to "

  • How do you append a char to a string in OCaml? 2011-11-30

    It seems as if there is no function in the standard library of type char -> string -> string, which insert a char in front of (or at the end of) a string. There are workarounds, e.g. by using String.make or String.blit. Is there an elegant way

  • C++ - How to append a char to char*? 2013-11-10

    I've tried so may ways on the Internet to append a character to a char* but none of them seems to work. Here is one of my incomplete solution: char* appendCharToCharArray(char * array, char a) { char* ret = ""; if (array!="") { char * ret = new char[

  • Is there any penalty between appending string vs char in C# 2010-08-02

    When developing in Java a couple of years ago I learned that it is better to append a char if I had a single character instead of a string with one character because the VM would not have to do any lookup on the string value in its internal string po

  • C - casting int to char and append char to char 2011-02-15

    I am making my first parallel application, but I am stuck with basics of C. I need to know, how to cast int to char and then how to append one char to another. It you could help me please, i would be glad. Thank you. --------------Solutions----------

  • string& and char* version of string::append(), string::replace() and string::compare() 2011-11-16

    The two overloaded function in std::string got my attention: string& append(const string& str, size_t pos, size_t n); string& append(const char* s, size_t n); I'm curious that why the char* version of string::append() doesn't provide an a

  • How to append data in JTextArea in Java? 2012-04-08

    I am using this code to show text in a JTextArea: jTextArea1.setText(jTextArea1.getText() + text + "\r\n"); jTextArea1.repaint(); But it shows an exception: java.lang.NullPointerException --------------Solutions------------- You never instantiated yo

  • Append Char To String in C? 2012-04-23

    How do I append a single char to a string in C? i.e char* str = "blablabla"; char c = 'H'; str_append(str,c); /* blablablaH */ --------------Solutions------------- char* str = "blablabla"; You should not modify this string at all. It resides in imple

  • Append data to an IStream object 2009-02-07

    I am making calls to a web service to get data from my dll. I am getting the data in a char* object in parts. I want to get the whole data into an IStream object. I am running a while loop and getting the data into char* object. Can someone please te

  • Overloading operator>> to a char buffer in C++ - can I tell the stream length? 2009-11-06

    I'm on a custom C++ crash course. I've known the basics for many years, but I'm currently trying to refresh my memory and learn more. To that end, as my second task (after writing a stack class based on linked lists), I'm writing my own string class.

  • Is there a way to catch un-displayable characters before they are written to a JTextArea? 2010-01-25

    I alter my keyTyped behaviour to default output the char to the JTextArea, however when I copy and paste, it produces an un-displayable character. I need this to work across multiple operating systems so using case 22: doesn't work as this doesn't ap

  • Only keep the legal chars in a text using a .NET Regex 2010-03-01

    I have a list of legal characters and I want to remove all others chars from text. // my legal chars. a-Z, numbers, space, _, - and percentage string legalChars = "[\p{L}\p{Nd}_\- %]*" string text = "[update], Text with {illegal} chars such as: !? {}

  • Add 2 chars without using strncpy? 2010-04-26

    How would I manually concatenate two char arrays without using the strncpy function? Can I just say char1 + char2? Or would I have to write a for loop to get individual elements and add them like this: addchar[0] = char1[0]; addchar[1] = char1[1]; et

  • NSString stringWithUTF8String appending error 2010-07-09

    I'm trying to append a char array to an NSString. When I use the code below, I get this error: *** initialization method -initWithCharactersNoCopy:length:freeWhenDone: cannot be sent to an abstract object of class NSCFString: Create a concrete instan

  • What are all possible linux path chars that need escaping 2010-11-05

    I'm wondering what are all the char needed escaping for a linux path. For example, path /home/user1/My Music/song 1.mp3 would need to be escaped in a shell for ls command 'ls /home/user1/My\ Music/song\ 1.mp3'. I want to write a function that takes i

Copyright (C), All Rights Reserved.

processed in 0.143 (s). 11 q(s)