Reputation: 7
I have created a JtextArea, and after having added a piece of text to the text area, want to cursor to be moved to the next line. I.e. I append the string to the text area, and when the user starts typing into the text area, want the input to be on the next line, not on the same line, at the end of the string I appended.
import javax.swing.JTextArea;
import javax.swing.JFrame;
public class NewSecureChat{
private JFrame myFrame;
private JTextArea chatArea;
public static void main(String[] args) {
public NewSecureChat() throws UnknownHostException {
JFrame myFrame = new JFrame();
myFrame.setSize(900, 400);
myFrame.setLocation(400, 400);
myFrame.setTitle("SecureChat 1.0" + (Inet4Address.getLocalHost().getHostAddress()));
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setAlwaysOnTop(true);
panel1 = new JPanel();
chatArea = new JTextArea("Chat here!", 3, 5);
panel5.add(chatArea);
chatArea.append("> Blank Message" + "\n");
}
}
}
Upvotes: 0
Views: 1776
Reputation: 324128
want to cursor to be moved to the next line.
chatArea.append("> Blank Message" + "\n");
chatArea.setCaretPosition( chatArea.getDocument().getLength() );
Upvotes: 2