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);
}

Similar Messages

  • How to Align Text in the TextArea

    How to Align text in the TextArea. My purpose is that I want to post the records after retrieving from database. All the records in each fields should keep aligned like Oracle WorkSheet.
    I want this way
    Code Name Place City
    a01 nilopher swiss street japan
    a02 rozina lovely street aus
    a03 benazir king's camp pitsburg
    and the out put should not look like this :-
    Code Name Place City
    a01 nilopher swiss street japan
    a02 rozina lovely street aus
    a03 benazir king's camp pitsburg
    here place and city records are getting disturbed accordingly the length of name.

    Well, first off (if it's not default, that is) you must set a monospaced font on the TextArea, unless you want to count pixelwidth of each and every string and then do an estimate on how many fill blanks you want...
    If you have the each string as a line, you use a StringTokenizer on it, extract each token, put all the tokens for that line into an array of strings.
    When every line is done, you compare the length of the nth String of each array and save the highest numbers.
    Then you reassemble each line from the arrays and pad in spaces where the tokens are too short.
    HTH,
    Fredrik

  • 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 set two colored text in one textarea

    hello friends,
    i am busy preparing chat frame which shows messages in textarea, it shows two things, one the name of chatter , the other is the message, i want the two things of different color, you can change the text color of textarea by setForeground(), but what if i want different colored text in same textarea, can anybody help me???

    I don't know if this will help u...try make sense of it :
    DefaultStyledDocument doc = new DefaultStyledDocument();
    JTextPane tp = new JTextPane(doc);
    tp.setFont (new java.awt.Font ("Arial", 1, 12));
    JScrollPane lscroller = new JScrollPane(tp);
    lscroller.setForeground (new java.awt.Color (102, 102, 153));
    lscroller.setPreferredSize (new java.awt.Dimension(496, 273));
    lscroller.setVerticalScrollBarPolicy(
    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    jPanel2.add ( lscroller );
    prepareAllStyles();
    void prepareAllStyles()
    aset = new SimpleAttributeSet();
    //set bold and red
    StyleConstants.setForeground(aset,Color.black);
    StyleConstants.setBold(aset,true);
    ht.put(BOLDRED,aset);
    aset = new SimpleAttributeSet();
    //set italic and green
    StyleConstants.setForeground(aset,Color.red);
    StyleConstants.setBold(aset,true);
    ht.put(ITALICGREEN,aset);
    AttributeSet current = (AttributeSet)ht.get(BOLDRED);
    doc.insertString(doc.getLength(),displaymessage + "\n",current);
    tp.setCaretPosition( doc.getLength() );

  • 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.

  • 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.

  • How to set text in a TextArea

    I have tried to find ways around this, but i must set text into a panel. but there is no .setText(), like there is for text fields. So how would i set text into a TextArea?
    BTW: what i am trying to do is be able to constatly add things without replacing what has been put up there.
    thank you

    BigDaddyLoveHandles wrote:
    sys5 wrote:
    I have tried to find ways around this, but i must set text into a panel. but there is no .setText(), like there is for text fields. So how would i set text into a TextArea?
    BTW: what i am trying to do is be able to constatly add things without replacing what has been put up there.
    thank youCould it be that you are not aware of the documentation?
    http://java.sun.com/javase/6/docs/api/
    By the way, JTextArea does have a setText method, but it replaces the current text with the given text. It sounds like you want to use ---- but wait, it's better if you find that out yourself. Big hint: it's the first method listed on JTextArea's API page.
    edit: too late!If he wasn't aware of the documentation, he probably would have tried using setText(), and found that it worked. That he thought it didn't exist suggests that he looked at the API but only looked in the method summary area, and not below at the "methods inherited from..." sections.
    (sorry to ruin your surprise)

  • How to align text at the top and bottom of a cell?

    I'm making a periodic table and need help with aligning text at the top and bottom of a cell. I'll have a picture in the middle of the cell with text above and below the pic. Thank you in advance for any kind of suggestions you can give me.

    It sounds as though you want to have 3 separate items inside of a single cell; text at the top of the cell, text at the bottom of the cell, and then a picture in the middle of the cell.  I am no expert, but to my knowledge that is not possible (someone please correct me if I am wrong.)  I also cannot figure out how to put a picture inside of a cell itself.
    I do have a way to accomplish the end result so long as what you need is the final look and not a useable table in numbers.
    Create 2-3 cells for each element.  (The middle cell, unless you can put pictures in a cell and I don't know, would just be there for peace of mind, but would hold the picture if you can, I would just do two if the pictures are to be in front of the cell anyway.)  The top cell align text to top on the Text tab of the inspector.  The bottom cell, align text to bottom on the same tab.  Then place the picture in the middle.  Now, you have what you want except there is one or two lines dividing the cells.  To get rid of this, either click the middle cell if you have one, or the top or bottom cell.  Click on the Cell tab of the inspector.  Select the bottom border and/or top border button and select "No Border" under border styles.  To make this fast, select a full row at a time, or use command click to select all of the same type of rows (middle, top, or bottom) and change all cells at once.
    I hope this helps.  Best of luck!
    ~Bret

  • How to align text to another text box

    I am very new to Indesign and I am signed up to Lynda.com so watching few a lot of tutorials, that said I need help with aligning text.
    I have a text box that is 100mm across. It has text centered. "£5"
    then above this I have in a smaller font saying "from"
    from
    £5
    I need to get the "f" of from to line up with the beginning of the £ sign? So that If I change the price to £500 the from sticks to the "£" Sign.
    To explain again the from needs to align to the left of the centred text.?
    Hope that make sense?
    Matt

    This can be done by putting the "from" text into a custom positioned anchored object just before the £ sign. You'll need to set the X coordinate relative to the anchor, and adjust the offset on both x and y to get the positioning where you want it.
    I'm sure there are lessons on Anchored Objects on Lynda.com.

  • 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

  • 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 !

  • Is it possible to use tabs to right align text in Muse, as you can in InDesign?

    Can I use tabs to right align text in Muse, as you can in InDesign?
    I have a beauty treatment followed by a price and I want to right align the price, keeping the treatment on the left of the text box.
    Thank you!

    For what would typically be a two column tab stop layout, I'd generally use inline text frames and the Wrap panel.
    Put the price in it's own text frame. Then cut and paste the text frame BEFORE the item name so it's an inline text frame within the text frame containing the descriptions.Using the Wrap panel set it to float to the right. Then select it and set the right offset so it floats outside the right side of the original text frame.
    Start with this.
    Cut and paste the text frame at the start of the item paragraph.
    Choose the third icon in the Wrap panel to cause the item to float to the right of the text frame.
    Turn off the lock for the 4 wrap offset values.
    Adjust the right offset to a negative value so the item is outside the text frame to the right (to wherever you want it).
    Repeat the same steps above for the other items.
    Note that once it's set up this way you can freely edit the descriptions or change the width of the original text frame and the prices will adjust accordingly. This will also result in things lining up in the browser even if the text layout engine of a specific browser line breaks the text differently.
    This approach is tedious, but the end result will continue to line up as you make changes in Design view and will line up in every browser/OS/device.
    Someday Muse will support tables, which would be the more natural way to achieve this style of layout on the web. Until then, inline items with wrap is usually the best approach for this type of two column layout.

  • How do I delete text from a textArea?

    How do I delete text from a textArea? I basically want the opposite of append(String)... I want something that removes a line from my TextArea. Anyone know?>

    If you want to remove only a specific part of the text, you have to get the String, take the parts before and after the removed part, append them back together and use setText to replace it in the textArea.

  • How do I format text within a TextArea

    Hello,
    I would like to create a simple text editor that performs
    syntax highlighting. In order to do so, I would need the ability to
    set the style of individual pieces of text within a TextArea. Is
    this possible? Do I need to subclass the TextArea (or perhaps the
    Text control) to do this? Any pointers in the right direction would
    be appreciated. Thanks!

    The issues around formatting text in the Flash Player are
    numerous. There are several experts who have weighed in on this,
    including .
    Axel
    Jensen who has made available a modification of the
    RichTextEditor component. Additionally,
    Jesse Warden has several older
    articles in his blog on this issue. Finally, there is a
    commercially available Flash/Flex text editor developed by Igor
    (last name unknown to me), and found at:
    http://www.flashtexteditor.com/.
    Overall, the issue of accurately formatting text in
    Flash/Flex has been an exercise in frustration, especially when
    trying to implement htmlText and CSS. I am absolutely amazed that
    Adobe's documentation for this very important aspect of web
    development is either unavailable or deeply flawed, and on top of
    that: How is the Acrobat Connect Buzzword application able to do so
    much with text formatting, clearly within a Flex Framework, and yet
    no concrete documentation of the techniques are available

  • How to get the text of the textArea ?

    Hi, expert
    i have created a new component com1,
    then i created a table view name 'result'.
    then i add a textarea by modifying the result.htm
    textArea id     = "TAMSG"
                     text   = "test......"
    then i add a button in the result.htm
    the method of the button is  'EH_ONSENDMSG'
    then i input some text into the textArea on the page.
    now, how can i get the text of the textArea in the method 'EH_ONSENDMSG' ?
    thanks.
    oliver.

    Hi,
    problem was solved.
    i put the text into a value node.
    <thtmlb:textArea id     = "TAMSG"
                     text   = "//TEXT/TEXT2"
                     encode = "TRUE"
                     width  = "100%" />
    method EH_ONSENDMSG.
      DATA lr_col_wrap      TYPE REF TO   cl_bsp_wd_collection_wrapper.
      DATA lr_query_values  TYPE REF TO   if_bol_bo_property_access.
      DATA lv_str_msg TYPE string.
      lr_col_wrap = me->typed_context->text->get_collection_wrapper( ).
      lr_query_values ?= lr_col_wrap->get_current( ).
      lv_str_msg = lr_query_values->get_property_as_string( iv_attr_name = 'TEXT2' ).
    endmethod.
    regards.
    oliver.

Maybe you are looking for

  • Separate domain name form name in email

    Hello, I  have an interactive report form which has email column. By default the email column is filled with [email protected] Now I need  to display that to be as  a.b (How do I remove domain name). How can I do that? Thanks in advance.

  • Value not visible in preview mode

    Hello there, On my dashboard, between other things, I have a simple grid component which should just show some resulted values. The problem I have is that when I connect the grid with the range of cells, everything looks fine but in preview mode, one

  • Windows 7 and Microsoft Office installed a series of updates and now I cannot open hyperlinks in emails.

    After my system rebooted and I opened my emails in outlook, when I clicked on a hyperlink, it brought me no where. I did not get an error message. I went back in and set Firefox to my default program with all extensions and protocols for Firefox. How

  • JScrollBar - Issue when changing models

    Hey All- Well, I've come up with another interesting question which I haven't been able to resolve myself. Basically, I have a single view port which consists of a custom JPanel and (2) JScrollbar's. I then have multiple data and view models which ar

  • Navigate to Transactional iVIEW by passing parameters & UWL

    Hi Experts, I have 2 queries here, 1>> I understand  that to navigate to a transaction from webdynpro a transactional iview has to be used. In my case i have to navigate to ME51 from the component and display the transaction with the details of a pat