Aligning Text in Center of JTextArea

This is a part of my previous question.
Is there a way of aligning text, left-center-right in the JTextArea.
Someone had mentioned JTextPane for aligning Text in the center. What is the code for this?
Thanks again for your help.

I'm sorry. I am new at Java so some of these concepts that may be clear to you aren't as obvious to me.
I inserted the code as you instructed. However, I am not sure if I have to include something else.
Please advise:
import javax.swing.*;
public class HelloClassTwoLine
public static void main(String args[])
JTextArea outputTextArea = new JTextArea(); //Create listbox object
JScrollPane scroll= new JScrollPane(outputTextArea); //create vertical scroll object
===> added to original code JTextField textField=new JTextField();
===> added to original code textField.setHorizontalAlignment(textField.CENTER);
outputTextArea.setText("Hello, This is Java 374\n" + //set text in list box
"This is Java 374 with Ravi");
JOptionPane.showMessageDialog(null, scroll, //text is argument in JScrollPane object
"Java Program #2", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
thank you again for your help
}

Similar Messages

  • Aligning Text in Center of JOptionPane

    Hi,
    I am trying to display messages using JOptionPane and a JDialog.
        JOptionPane pane = new JOptionPane(message,
                                           message_type,
                                           option_type,
                                           icon,
                                           options,
                                           initial_value){
          public int getMaxCharactersPerLineCount() { return    default_characters_per_line; }
        JDialog dialog = pane.createDialog(parent_component, title);
        dialog.setVisible(true);I want to be able to control the number of characters per line which I have found how to do, and to control the alignment of the text. I have not being able to work out how to control the alignment of the text. I would like to be able have the text centered so a long line followed by a short line is displayed nicely.
    I have tried using <html><center>My Text</center></html>, however, 1stly the html tags are counted as character therefore shortening the 1st line and secondly is the message is over several line then the tag are broken up and not interpreted properly.
    Any assistance greatly appreciated.
    Leon

    //Create the dialog.
                        final JDialog dialog = new JDialog(frame,
                                                           "A Non-Modal Dialog");
                        //Add contents to it. It must have a close button,
                        //since some L&Fs (notably Java/Metal) don't provide one
                        //in the window decorations for dialogs.
                        JLabel label = new JLabel("<html><p align=center>"
                            + "This is a non-modal dialog.<br>"
                            + "You can have one or more of these up<br>"
                            + "and still use the main window.");
                        label.setHorizontalAlignment(JLabel.CENTER);
                        Font font = label.getFont();
                        label.setFont(label.getFont().deriveFont(font.PLAIN,
                                                                 14.0f));extract from
    http://java.sun.com/docs/books/tutorial/uiswing/examples/components/DialogDemoProject/src/components/CustomDialog.java

  • How to align text(left, right or center) on a button?

    I can not figure out how to align text on a button,
    could someone help me out?
    Thanks in advance

    JButton btn=new JButton("hi");
    btn.setHorizontalAlignment(int i)
    i=0 center
    i=2 left
    i=4 right

  • Aligning text in JTextArea

    Hi,
    Does anyone know how to set the point at which text in displayed in a JTextArea using the setText() method. I am trying to align text in the centre of a JTextArea. Is there a way one can find the pixel at the centre of the textarea and then somehow set the cursor point to the centre and to align the string correspondingly?

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class PlacingText {
      static int hInset = 20;
      public static void main(String[] args) {
        String text = "public class JTextPane \n" +
          "extends JEditorPane \n\n " +
          "A text component that can be marked up with attributes that are represented " +
          "graphically. You can find how-to information and examples of using text panes " +
          "in Using Text Components, a section in The Java Tutorial. \n\n" +
          "This component models paragraphs that are composed of runs of character level " +
          "attributes. Each paragraph may have a logical style attached to it which " +
          "contains the default attributes to use if not overridden by attributes set on " +
          "the paragraph or character run. Components and images may be embedded in the " +
          "flow of text.";
        final JTextArea ta = new JTextArea(10,40);
        ta.setMargin(new Insets(10,hInset,10,hInset));
        ta.setLineWrap(true);
        ta.setWrapStyleWord(true);
        ta.setText(text);
        JPanel panel = new JPanel();
        panel.add(new JScrollPane(ta));
        final JButton moreButton = new JButton("more");
        final JButton lessButton = new JButton("less");
        ActionListener l = new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            JButton button = (JButton)e.getSource();
            if(button == moreButton)
              hInset += 10;
            if(button == lessButton)
              hInset -= 10;
            ta.setMargin(new Insets(10,hInset,10,hInset));
            ta.repaint();
        moreButton.addActionListener(l);
        lessButton.addActionListener(l);
        JPanel southPanel = new JPanel();
        southPanel.add(new JLabel("horizontal margin:"));
        southPanel.add(moreButton);
        southPanel.add(lessButton);
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(panel);
        f.getContentPane().add(southPanel, "South");
        f.pack();
        f.setLocation(300,300);
        f.setVisible(true);
    }

  • How to right align text in JTextArea

    Can u plz tell me how to right align text in JTextArea

    what do you mean by align text in java you mean you have a default text that align in the center is it?
    can you please explain ur question clearly
    Thanks

  • Problem with center aligned text when publishing

    Can anyone help me with this? I'm a bit desperate.
    I created blocks of text and center aligned them:
    Everything looked great but when I published all the text appeared left aligned.
    I tried everything but nothing seems to resolve this.
    Thank you in advance!
    Best regards,
    Paulo Moura

    Hi,
    Try exporting your file with the following settings to get a high quality GIF
    Dimensions   : Match movie
    Playback       : Animated
                        : Loop continuously
    Options         : Optimize colors
                        : Smooth
    Transparent   : Opaque
    Dither           : None
    Palette Type : Adaptive
    Max colors   : 99999
    Thanks!
    ps: please mark this post as Answered if this of any help to you

  • Problems Aligning Text in Headers and Footers

    I have tried to add headers and footers to documents and the  alignment is wrong.  The center footer/header should be center-aligned  and the right header/footer should be right-aligned.  Unfortunately,  they are both left-aligned.
    The preview shows the headers and footers aligned correctly, but when  the headers/footers are inserted in the document, the alignment reverts  to left aligned, although it is centered and on the right side of the  page, as appropriate.
    This is not a problem in Acrobat 7.
    Please advise if this is a bug and is awaiting an update, or if there is  something that I need to do to fix this problem.
    This is a screen shot of the preview windowindicating the proper alignment as indicated in the ovals:
    Here is the actual document once the header and footer are added.  Notice how the alignment switches to flush left:
    Any guidance is appreciated.
    Thanks.
    - James

    Nope.  The good folks at Adobe have neither addressed this issue (and I have
    heard from others that it is a problem for them as well) nor have they been
    very good about maintaining my account to be able to log in and follow up on
    it.
    The truth of the matter is that it appears to be a glitch in some, but not
    all, versions of this product and clearly it is not a big enough problem for
    them to want to fix it.
    I wind up using Acrobat 7 when I need properly formatted headers and footers
    or ‹ and this ought to get the attention of the folks at Adobe and maybe
    prompt someone (anyone!) to take a look and fix this glitch ‹ you should
    check out PDFPen.  It does much of what Adobe can do including headers and
    footers.
    If I didn't already have Acrobat 7 (and 8 and now 9), I would ditch Acrobat
    and just use Preview or PDFPen.
    - James
    From:  superfluities1 <[email protected]>
    Reply-To:  <[email protected]>
    Date:  Tue, 04 Jan 2011 12:11:19 -0700
    To:  "James A. Sarna, Esq." <[email protected]>
    Subject:  Problems Aligning Text in Headers and Footers
    Anybody ever solve this problem? I just started using(trying too, that is)
    Acrobat Pro for writing letters and starting at the top I ran right into
    this problem. Seems like a deal killer.

  • Cannot Align text in cell

    Greetings,
    I am having issues when trying to format my spreadsheet.  First of all, I have "Wrap Text in Cell" option turned on.  So when I have text that can fit into the cell, its fine.  But when I have long text in the cell, it wraps the text in the cell so it can fit inside, which I'm fine with.... but for some reason, it is not aligning it in the cell properly, so now part of the text it cut off.  I try to center align the text, but no use. I hit the button for "Align text in the middle of the table cell", and still does not align the text so that its centered, not cut off. I have included an image of what im talking about. Help please?

    Hello
    Here is what I get with Times New Roman under 10.6.8
    May you try to
    quit Pages
    trash the preferences file :
    <startupVolume>:Users:<yourAccount>:Library:Preferences:com.apple.iWork.Pages.pl ist
    restart Pages
    It it changes nothing, try to :
    quit Pages
    trash the preferences file :
    <startupVolume>:Users:<yourAccount>:Library:Caches:com.apple.iWork.fonts
    restart Pages
    Yvan KOENIG (VALLAURIS, France) mercredi 24 août 2011 21:24:52
    iMac 21”5, i7, 2.8 GHz, 4 Gbytes, 1 Tbytes, mac OS X 10.6.8 and 10.7.0
    My iDisk is : <http://public.me.com/koenigyvan>
    Please : Search for questions similar to your own before submitting them to the community
    To be the AW6 successor, iWork MUST integrate a TRUE DB, not a list organizer !

  • Align text in a textarea

    Greetings,
    Could anyone point me out in how to align a text in a textarea?, as in left/right/center ?
    Thank you

    An easy way to do it is to use a JEditorPane... here's an example class:
    package com.clearlyhazy;
    import javax.swing.*;
    import java.awt.*;
    public class AlignableTextPane extends JEditorPane {
         private static String LEFT = "\"LEFT\"";
         private static String CENTER = "\"CENTER\"";
         private static String RIGHT = "\"RIGHT\"";
         private String alignString = LEFT;
         private String realText;
         public AlignableTextPane() {
              super("text/html", "");
         public AlignableTextPane(String align) {
              super("text/html", "");
              setAlignment(align);
         public void setAlignment(String align) {
              if (align.equals(AlignableTextPane.LEFT) || align.equals(AlignableTextPane.CENTER) || align.equals(AlignableTextPane.RIGHT)) {
                   alignString = align;
         public String getText() {
              return realText;
         public void setText(String t) {
              realText = t;
              super.setText("<P ALIGN=" + alignString + ">" + realText + "</P>");
         public void setText(String t, String alignment) {
              alignString = alignment;
              setText(t);
         public static void main(String[] args) {
              JFrame tester = new JFrame("Alignment Test");
              JPanel mainPanel = new JPanel();
              mainPanel.setLayout(new GridLayout(1, 0));
              AlignableTextPane leftPane = new AlignableTextPane(AlignableTextPane.LEFT);
              leftPane.setText("This text is left-aligned.");
              mainPanel.add(leftPane);
              AlignableTextPane centerPane = new AlignableTextPane(AlignableTextPane.CENTER);
              centerPane.setText("This text is center-aligned.");
              mainPanel.add(centerPane);
              AlignableTextPane rightPane = new AlignableTextPane(AlignableTextPane.RIGHT);
              rightPane.setText("This text is right-aligned.");
              mainPanel.add(rightPane);
              tester.setContentPane(mainPanel);
              tester.setSize(400, 400);
              tester.setLocationRelativeTo(null);
              tester.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              tester.setVisible(true);
    }

  • Aligning text in JLabel

    Hi all,
    I'm simply trying to align text in a JLabel.
    I've been trying to use HTML formatting inside the JLabel, e.g:
    label = new JLabel (" <HTML><CENTER><P>Line one</P> <P>Line two</P></CENTER> </HTML>" );but when I try to use the <CENTER> tag it seems to be ignored?
    Why?
    How do I get around this?
    Many thanks.

    You can use the following code segment:
    JLabel ab = new JLabel("GG" ,  SwingConstants.CENTER);Hope it helps.
    --DM                                                                                                                                                                                                                                                                       

  • Aligning text in a text area

    Is it possible to align text in a JTextArea?

    also not sure what you're after, but try this
    import javax.swing.*;
    import java.awt.*;
    class Testing extends JFrame
      public Testing()
        setLocation(300,200);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        String[] name = {"joe","christopher","fred","robert","al","josephine","mary"};
        String[] income = {"100","100000","10000","1","88888","50000","200000"};
        JPanel jp = new JPanel();
        JTextArea ta = new JTextArea(20,30);
        JScrollPane sp = new JScrollPane(ta);
        sp.setPreferredSize(new Dimension(175,150));
        jp.add(sp);
        getContentPane().add(jp);
        pack();
        String pad = "                    ";
        ta.setFont(new Font("monospaced",Font.PLAIN,12));//comment out this line to see difference
        for(int x = 0; x < name.length; x++)
          ta.append(name[x]+(pad+income[x]).substring(name[x].length()+income[x].length())+"\n");
      public static void main(String[] args){new Testing().setVisible(true);}
    }

  • Aligning text in a button?

    I have a button (a simple rectangle) and I create a new layer and place text on the first keyframe while in Button Symbol Edit Mode. I now want to align the text in the center of the button. I drag over both the text and the button to select them both and than I open the Align Panel and I choose to align in the center. However, it is not aligning the text in the center and it is also moving the button to the left.
    What is the correct way to do this?
    Thanks.

    Sorry for not getting this. I'm coming from Illustrator, and there I can select the background shape and text, select the shape as a key object and align the text both vertically and horizontally inside the shape.
    I can not align the text vertically in the shape in FL. If I have both the shape and text selected, do I work with the align panel or the property inspector to do this?
    Thank you for your patience.

  • Left-aligning text in Final Cut 7

    I am using Final Cut Pro Version 7.0.3, doing mostly subtitles for TV series.
    Because of the amount of titles done daily, and because we left-align around 30% of all our subtitle text, I would save a lot of time if there was a keyboard shortcut to left-align text in the subtitle clips.
    Text Settings under the Control tab in the Viewer, automatically chooses Center Alignment, which is fine, but how can I left-align text without having to choose from the Alignment options? I could not find this option in the keyboard layout, so I could not assign a key for left-alignment.
    Does anyone know how to do this?

    This is not a direct answer to your question, but in my experience most FCP-7 users don't use the Text option in text intensive projects. As you have discovered, the Text options are limited.
    Instead they use Boris Title 3D which is bundled in FCP-7:
    Boris Title 3D gives you much more control over your text, and defaults to left justification:
    You might take a look and see if using this tool (which Apple provided to address the shortcomings of the Text generator) can help you.
    MtD

  • Vertically align text

    Hi
    Is it possible to vertically align text within a frame?
    Thanks

    Wade_Zimmerman wrote:
    I really do not think there is a reason to do this but if the OP has a good reason or wants to tell us why they need this we might be abe tooffer them a better way.
    I searched for a solution to this myself and missed this thread before i posted my own thread. but I find it odd how when this is asked for people are asked to give a good reason to use the bounding box, or like here told that there is no reason to need this. Well here is my current situation.
    I have a project that has 500+ peices. I am using data sets and variables.xml  to create them all quickly.
    Each one consists of a 1 of  three logos and 1 of 500+ lines of text and all the lines of text are  different lengths
    Now the problem I am having is getting the  text to wrap properly instead of running off the art board. This was easilly solved by using a text box. Now I am faced with a new issue, One line texts are all sitting really high on the artwork.
    So I would like to center all text vertically inside the box

  • How can I align text in Acrobat 8.1 Professional?

    Okay this is quite silly, but seriously how can I align text in adobe acrobat? I could not find a way other than creating a new document file which allows me to use alignment options automatically (example below), however does not allow me that when I reopen the same file (alignment and text options just dissapear on the bars, funny). Help anyone please? =/
    http://img152.imageshack.us/my.php?image=examplemo2.jpg

    Not that silly; but you ideally shouldn't be trying. Although there are some rudimentary (and sometimes unpredictable) editing tools in Acrobat, (I presume you are trying with the TouchUp Object tool as well as the TouchUp Text ?) much better to get it right in the source application and think of the pdf as what it basically is...a print.
    For whatever reason, v5 could edit text positioning rather more easily than the later versions.

Maybe you are looking for