You can't use a JTextArea to change the alignment of individual lines of text.
To change attributes of individual lines the easiest way is to use a JTextPane
. Something like:
JTextPane textPane = new JTextPane();
StyledDocument doc = textPane.getStyledDocument();
SimpleAttributeSet left = new SimpleAttributeSet();
StyleConstants.setAlignment(left, StyleConstants.ALIGN_LEFT);
StyleConstants.setForeground(left, Color.RED);
SimpleAttributeSet right = new SimpleAttributeSet();
StyleConstants.setAlignment(right, StyleConstants.ALIGN_RIGHT);
StyleConstants.setForeground(right, Color.BLUE);
try
{
doc.insertString(doc.getLength(), "\nLeft aligned text.", left );
doc.setParagraphAttributes(doc.getLength(), 1, left, false);
doc.insertString(doc.getLength(), "\nRight aligned text.", right );
doc.setParagraphAttributes(doc.getLength(), 1, right, false);
doc.insertString(doc.getLength(), "\nMore left aligned text.", left );
doc.setParagraphAttributes(doc.getLength(), 1, left, false);
doc.insertString(doc.getLength(), "\nMore right aligned text.", right );
doc.setParagraphAttributes(doc.getLength(), 1, right, false);
}
catch(Exception e) {}