Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: Center AND wrap text

  1. #1
    Megabuffen is offline Member
    Join Date
    Dec 2011
    Posts
    1
    Rep Power
    0

    Unhappy Center AND wrap text

    Hi!

    I'm trying to display text in a window. I want it to be centered and to wrap onto multiple lines. I can do both, but not at the same time. For example, a JLabel supports centering but not wrapping, while a JTextArea supports wrapping but can't center text.

    How do I do this?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Center AND wrap text

    You can kind of cheat and do this using a JLabel with HTML and css style statements in the text. For instance:

    Java Code:
    import java.awt.Font;
    import javax.swing.*;
    
    public class WrapCenterText {
       public static final String HTML_1 = "<html><body style='width: ";
       public static final String HTML_2 = "px; text-align:center'>";
       public static final String HTML_3 = "</html>";
       public static final int WRAP_WIDTH = 500;
    
       private static final String GETTYSBURG = "Four score and seven years ago our fathers brought forth " +
       		"on this continent a new nation, conceived in liberty, and dedicated to the proposition that " +
       		"all men are created equal.<br><br>"
             + "Now we are engaged in a great civil war, testing whether that nation, or any nation, so " +
             "conceived and so dedicated, can long endure. We are met on a great battle-field of that war." +
             " We have come to dedicate a portion of that field, as a final resting place for those who " +
             "here gave their lives that that nation might live. It is altogether fitting and proper " +
             "that we should do this.<br><br>"
             + "But, in a larger sense, we can not dedicate, we can not consecrate, we can not hallow " +
             "this ground. The brave men, living and dead, who struggled here, have consecrated it, " +
             "far above our poor power to add or detract. The world will little note, nor long remember " +
             "what we say here, but it can never forget what they did here. It is for us the living, " +
             "rather, to be dedicated here to the unfinished work which they who fought here have thus " +
             "far so nobly advanced. It is rather for us to be here dedicated to the great task " +
             "remaining before us—that from these honored dead we take increased devotion to that " +
             "cause for which they gave the last full measure of devotion—that we here highly resolve " +
             "that these dead shall not have died in vain—that this nation, under God, shall have a " +
             "new birth of freedom—and that government of the people, by the people, for the people, " +
             "shall not perish from the earth.";
    
       private static void createAndShowGui() {
          String text = HTML_1 + String.valueOf(WRAP_WIDTH) + HTML_2 + GETTYSBURG + HTML_3;
          JLabel label = new JLabel(text);
          label.setFont(label.getFont().deriveFont(Font.BOLD, 20));
    
          JFrame frame = new JFrame("WrapCenterText");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(label);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Center AND wrap text

    Or use a JEditorPane if you want the text to be editable. Here, with a JTextPane:

    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextPane;
    import javax.swing.SwingUtilities;
    import javax.swing.text.SimpleAttributeSet;
    import javax.swing.text.StyleConstants;
    import javax.swing.text.StyledDocument;
     
    public class WrapCenterText2 {
       public static final int WRAP_WIDTH = 500;
     
       private static final String GETTYSBURG = "Four score and seven years ago our fathers brought forth " +
            "on this continent a new nation, conceived in liberty, and dedicated to the proposition that " +
            "all men are created equal.\n\n"
             + "Now we are engaged in a great civil war, testing whether that nation, or any nation, so " +
             "conceived and so dedicated, can long endure. We are met on a great battle-field of that war." +
             " We have come to dedicate a portion of that field, as a final resting place for those who " +
             "here gave their lives that that nation might live. It is altogether fitting and proper " +
             "that we should do this.\n\n"
             + "But, in a larger sense, we can not dedicate, we can not consecrate, we can not hallow " +
             "this ground. The brave men, living and dead, who struggled here, have consecrated it, " +
             "far above our poor power to add or detract. The world will little note, nor long remember " +
             "what we say here, but it can never forget what they did here. It is for us the living, " +
             "rather, to be dedicated here to the unfinished work which they who fought here have thus " +
             "far so nobly advanced. It is rather for us to be here dedicated to the great task " +
             "remaining before us—that from these honored dead we take increased devotion to that " +
             "cause for which they gave the last full measure of devotion—that we here highly resolve " +
             "that these dead shall not have died in vain—that this nation, under God, shall have a " +
             "new birth of freedom—and that government of the people, by the people, for the people, " +
             "shall not perish from the earth.";
     
       private static void createAndShowGui() {
          JTextPane textPane = new JTextPane();
          StyledDocument doc = textPane.getStyledDocument();
          SimpleAttributeSet center = new SimpleAttributeSet();
          StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
          doc.setParagraphAttributes(0, doc.getLength(), center, false);
          textPane.setText(GETTYSBURG);
     
          JFrame frame = new JFrame("WrapCenterText (Editable)");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(new JScrollPane(textPane));
          frame.setSize(500, 400);
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
     
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    Fubarable likes this.

Similar Threads

  1. How to left justify and wrap text
    By Tejas in forum New To Java
    Replies: 12
    Last Post: 10-05-2011, 01:27 AM
  2. How to wrap text in a JList
    By applewood13 in forum New To Java
    Replies: 4
    Last Post: 05-11-2011, 03:48 PM
  3. Center text in a JComponent?
    By Zman3359 in forum New To Java
    Replies: 4
    Last Post: 04-17-2011, 11:44 PM
  4. word wrap text in rows of Jtable
    By pink123 in forum AWT / Swing
    Replies: 3
    Last Post: 03-10-2011, 08:20 PM
  5. Replies: 0
    Last Post: 09-28-2010, 10:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •