Yeap
Yeap

Reputation: 1

Scrollable, uneditable, updatable text

I'm new to Java and I started a little RPG game. When a battle starts, I would like to display the battle messages in a little box. I would like the box to be scrolled automatically, displaying the new message every time, and not losing the old messages.

Upvotes: 0

Views: 48

Answers (1)

achref
achref

Reputation: 1220

I am going to give you the information you need, but it is your duty to figure out how to use them: 1-you should use a JTextArea to display your messages. 2-when a new message come, use the append() function on you JTextArea object (use \n to return automatically in line). 3-add a JScrollPane to your JTextArea so it can be scrollable. 4-update you caret automatically to always show the last message printed use this where myJTA is your JTextArea:

DefaultCaret caret = (DefaultCaret)myJTA.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

now you have all the pieces of the puzzle. Good luck.

EDIT: if you want your JTextArea not editable use:

myJTA.setEdtable(false);

Upvotes: 1

Related Questions