0

I'm building a text-based adventure game, and I'm trying to make it so that the computer's responses are on the left, while the choices you make appear on the right, so as to easily distinguish the two. The problem is, I can't seem to find a way to align text to the right. I'm using a JTextArea inside a JScrollPane, all inside a JFrame.

Help is much appreciated, Thanks. :)

Bom Tomdabil
  • 61
  • 1
  • 10
  • Possible duplicate of: http://stackoverflow.com/questions/24315757/java-align-jtextarea-to-the-right – KevinO Apr 04 '16 at 18:08
  • @KevinO I tried those, but it doesn't work. Could it be because I'm using the `append(String)` method? – Bom Tomdabil Apr 04 '16 at 18:18
  • There is insufficient provided information as to exactly how you are attempting to achieve your result. You need to share succinct code that clearly demonstrates the issue and clearly explain the expectations and the failures. However, if your point is that you are attempting in the same text area to left and right align text, you will have to format the reply string to be right justified. The Apache Commons has utilities to help. I would personnally use a JScrollPane and append new Widgets for each part of the conversation, some left justified, some right justified. – KevinO Apr 04 '16 at 18:21
  • You will have to use a JEditorPane or JTextPane to do this. Perhaps a simpler way would be to use two side-by-side JTextAreas, or a JList or a 2-column JTable – FredK Apr 04 '16 at 18:30

2 Answers2

3

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) {}
camickr
  • 321,443
  • 19
  • 166
  • 288
  • When I try to compile, it says it can't find the variable `StyleConstants` here: `StyleConstants.setAlignment(left, **StyleConstants**.ALIGN_LEFT);` but not the one at the beginning. Am I missing something? – Bom Tomdabil Apr 05 '16 at 01:33
  • Read the API for the `StyleConstants` class to find out what package you need to import. – camickr Apr 05 '16 at 01:37
1
jTextArea1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

Try this code :) put this code in constructor .

Shinwar ismail
  • 299
  • 2
  • 9