-2

I've been for a while searching through here and other Java forums. Also googling it, but I haven't found anything that matches my expectations (a line break, basically). I've achieved this:

public final void messageRoom (String message, Boolean bold, Color color) {

    StyledDocument document = new DefaultStyledDocument();
    SimpleAttributeSet attributes = new SimpleAttributeSet();

    if(bold) {
        attributes.addAttribute(StyleConstants.CharacterConstants.Bold, Boolean.TRUE);
    }
    attributes.addAttribute(StyleConstants.CharacterConstants.Foreground, color);

    try {
        document.insertString(document.getLength(), message, attributes);
    } catch (BadLocationException ex) {
        System.out.println("ex");
    }

    chatArea.setStyledDocument(document);
}

That allows me to send messages to a chat room I'm creating, how can I make the line break to go to the next line?

Thank you all! (Similar but not equal posts: First post and The second one)

Community
  • 1
  • 1
Mario
  • 19
  • 6
  • This isn't how StackOverflow works. You do not just post cool stuff you figured out, as a question. Describe the problem clearly and succinctly, with code example, etc. in your question. If you also have the answer, post it _as an answer_, separate from the question, and then after the required delay, ACCEPT your answer. We don't put "SOLVED" in the title, and don't put answers into the question. Please visit the [help] and read [ask] and [answer]. – Jim Garrison Jan 23 '17 at 02:33

1 Answers1

1

how can I make the line break to go to the next line?

Maybe I don't understand the question. The text in a text pane will "wrap" automatically.

If you are attempting to start each message on a new line then you just use "\n" as the new line character.

Maybe something like:

document.insertString(document.getLength(), "\n" + message, attributes);

Of course you wouldn't want to add a new line for the first message.

public final void messageRoom (String message, Boolean bold, Color color)

Don't use an Object when a primitive variable will do. Just use a "Boolean" parameter.

camickr
  • 321,443
  • 19
  • 166
  • 288