Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-2.1· AnthropicHugging Face Arena Human Preference 55K

Can you center text in a jtextarea?

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Upstagesolar-10.7b-instruct-v1.0’s answer.
  1. User

    Can you center text in a jtextarea?

  2. Claude Assistant

    Yes, you can center text horizontally in a JTextArea in a few ways in Java:

    1. Use setHorizontalAlignment():
    JTextArea textArea = new JTextArea();
    textArea.setHorizontalAlignment(JTextArea.CENTER);
    
    1. Set the paragraph attributes to centered:
    StyledDocument doc = textArea.getStyledDocument();
    SimpleAttributeSet center = new SimpleAttributeSet();
    StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
    doc.setParagraphAttributes(0, doc.getLength(), center, false);
    
    1. Use HTML to center the text by setting the text to:
    textArea.setText("<html><center>Centered text</center></html>");
    

    So in summary, you can center the text in a JTextArea using the setHorizontalAlignment() method, by setting paragraph attributes, or by using HTML.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.