Trouble with text boxes

Hey guys, is there a common way to handle text boxes easily in java?
I’m making a sort of RPG and of course you’ll need to be able to walk up to NPCs and talk to them.
I’m thinking of using a traditional textbox…the best example I could think of is like the textboxs in the pokemon games. Where it shows a few lines of text and then you press a button to go to the next few lines of text. A scroll box would be nice, but it doesn’t really matter to me.

Anyways my current idea is a mess.
Right now I’m trying read the text one word at a time and storing each word into a variable like word1,word2,word3,word4,word5 and making another variable thats is for lines, such as line1,line2,line3, where the first 5 words go into line1, the next 5 go into line2 and the next 5 go into line3.
After that if a button is pressed than it starts drawing the next few lines until there’s no text.

Anyways like I said, I feel like I’m doing it pretty backwards and came here to see if there’s an easier method to all this. (: thanks!

Can you use a MouseListener on the text box or enclosing rectangle bounds to check for clicks?

It should be possible to write a reusable function to extract the “next” text from a larger text block. No need to store individual words.

The function would need an index or cursor to hold the position of the last character currently visible, and the length of the text to be extracted. Within the method there should be a way to work backwards to find the nearest space character so that you don’t break words in half.

Or, write or use a stream that breaks at spaces–isn’t there a stream that already does that?–and add ‘words’ to a StringBuffer until the “next” one would pass the limit.

A “wrapper” object could hold the last displayed char in an instance variable.

Do you need a code sample, or can you write this yourself?

I believe I understand what your saying, but an example would be extremely helpful if you dont mind (:

What part are you having trouble with?

This String method lastIndexOf(int ch, int fromIndex) should be helpful. You can use it to look backwards from the maximum number of chars that can fit, for a space, and then use the return value when you extract the “next” substring.
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#lastIndexOf(int,%20int)

If you haven’t already done so, maybe check out the descriptions and methods for both String and StringBuffer. They are fundamental knowledge, very helpful to know about! (Good to know how they are different, when to use which one.)

Have you looked at the java tutorials on String and StringBuilder?
http://docs.oracle.com/javase/tutorial/java/data/manipstrings.html
http://docs.oracle.com/javase/tutorial/java/data/buffers.html

The StringBuilder would be something you’d use to store results of appending if you used the algorithm of adding “words” progressively. But an algorithm using the lastIndexOf() method seems like a better choice to me. Maybe either approach is fine. An I/O stream approach would make more sense if the data for the boxes were stored as assets in files?