I have a few text areas that are filled with loops of information. Is it possible to make it 'jump' or scroll back to the top automatically after the loop has completed, so that the user sees it from the start and not the end?
You can use setCaretPosition() to set the cursor at position 0 of the TextArea. That usually works for me.
Use setUpdatePolicy with NEVER_UPDATE, and make sure the setUpdatePolicy comes before setting any text to the JTextArea. In my example I initialize a JTextArea without any text and call setUpdatePolicy right after.
JTextArea jTextArea = new JTextArea();
((DefaultCaret)jTextArea.getCaret()).setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
I have solved this problem by creating a new TextArea which acts as a sort of buffer (and isn't displayed). The loop writes to this area and when completed gets the text from that and sets it to the required TextArea.
Related
Take a look at this picture:
This is a JLabel with a height that is not allowed to vary. However when there is too much text to fit in its given size it attempts to display both lines. I do not want this, how do I make it so that only the first line up to the "in new" is displayed?
Why not make it scrollable? This way you can maintain the preferred size, and at the same time display the info when it's needed. You just have to put the JLabel inside a JScrollPane:
JScrollPane scroller = new JScrollPane(yourJLabel,
JScrollPane.VERTICAL_SCROLLABAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLABAR_AS_NEEDED);
The solution was to not use html (ie, "<html>" inside of the string) for it was what was causing the lines to warp when too long. All bolding and monospacing had to be done without using html.
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.
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);
I have a JTextField that a User and an Admin update to try and solve a problem that a user may have. Although when I setText(conversation) to the JTextField the text starts in the middle and when it goes to the end of a line it does not go to a different line. Can I set the JTextField so that text begins at the top? and when the user/admin gets to the side of the text field it goes to another line? I have tried looking this up everywhere and can not find an answer
here is a picture of the window with the conversation text field
Just noticed that you are using a JTextField. JTextField is a single line and will hence print the text that appears to be center aligned. You should be using a JTextArea instead that that is multi-lineand it will solve your problem
In JFrame Form I have added a jTextArea and added a text into it, while still in design mode. But when I run the form I want the text to be invisible, which is not happening. I checked the properties of the JTextArea but I am not really sure how to change the visiblity of the text..
For clearing the JTextArea filed you need to set its text as empty.
For this you need to see JTextArea#setText as null or "".Try this code at the execution of your application.
jTerxtAreaObject.setText("");
Set the color of the text as the same color as the background.
I have a java Box in which are various components. When the user performs an action, I want to empty the box, put a different set of components in it and then represent the box to the user. The box is part of a JDialog box. I am able to empty the box and repopulate it, but the display does not reflect the new contents. It is simply a blank display (I assume reflecting the fact that I emptied the contents of the box). I repaint the box after I put the new contents in but the display is blank in the area where the box is. Oddly enough, if I first add a line border to the box, then repaint it, the contents of the Box appear on the screen. This is most bizarre behavior. Any ideas why this is happening and how to get around this problem?
Thanks,
Elliott
Are you adding and removing the contents from the EDT? if this is occuring on the wrong thread weird repaint behavior can result.
Also might want to try
dialog.revalidate()
if the above is not the issue instead of calling repaint