Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML


 

Java Tutorials


 

 

Struts Tutorials

Struts Resources

Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Java: JTextArea

Description

A javax.swing.JTextArea is a multi-line text component to display text or allow the user to enter text. For one line of text use JTextField.

Constructors

JTextArea ta = new JTextArea(int rows, int cols);
JTextArea ta = new JTextArea(String initialText, int rows, int cols);
 
 
 
 
 
 
 
 
 
 
 
 
 

Using a JTextArea for output -- Setting the text

Assume: JTextArea ta; int i, w, pos, start, end, line; String s; boolean b; Font f;

ResultMethodDescription
Setting text
 ta.setText(s);Replaces all text with s.
 ta.append(s);Appends s to the end.
 ta.insert(s, pos);Inserts s at position pos.
 ta.replaceRange(s, start, end);Replace start to end with s.
Getting text
s = ta.getText();Returns all text. Use methods below to get individual lines.
i = ta.getLineCount();Returns number of lines in the text area.
i = ta.getLineStartOffset(line);Returns character index of beginning of line line. May throw javax.swing.text.BadLocationException.
i = ta.getLineEndOffset(line);Returns character index of end of line line. May throw javax.swing.text.BadLocationException.
Changing the appearance/function
 ta.setBorder(brdr);Text is tight against edge. See example below to add space.
 ta.setLineWrap(b);Lines wrapped if true. Default false.
 ta.setWrapStyleWord(b);If wrapping on (see above), wraps at words (true) or chars (false). Default false.
 ta.setTabSize(w);Number of max width chars in a tab.
 ta.setFont(f);Displays using Font f.
 ta.setEditable(b);Set false to disable user editing.
 ta.setCaretPosition(i); Set caret position. If content is scrolled, setCaretPosition(0) will move to top.

Getting and setting all lines sequentially

The following code shows how to sequentially get and set each line in a JTextArea Assume inputArea has the input and outputArea will receive the output. Of course, this is a ridiculous way to copy from one text area to another; it would be better to just write outputArea.setText(inputArea.getText());, but it wouldn't illustrate working with individual lines.

JTextArea inputArea  = new JTextArea(40, 20);
JTextArea outputArea = new JTextArea(40, 20);
. . .   
    outputArea.setText("");           // Empties the textarea
    
    String text = inputArea.getText();
    int totalLines = inputArea.getLineCount();
    for (int i=0; i < totalLines; i++) {
        int start = inputArea.getLineStartOffset(i);
        int end   = inputArea.getLineEndOffset(i);
        String line = text.substring(start, end);
        
        outputArea.append(line + "\n");
    }

Scrolling

JTextArea doesn't support scrolling itself but you can easily add the JTextArea to a JScrollPane. JScrollPane creates scrollbars as needed. For example,

//--- Create scrolling text area.
resultTA = new JTextArea("This is a test", 10, 80);
JScrollPane scrollingResult = new JScrollPane(resultTA);
content.add(scrollingResult);

Positioning the view to the top - moving the caret

If more lines are added than can be displayed and the scrollbars appear, the position that shows can be controlled with setCaretPosition(pos), where pos is a character position between 0 and the length of the text. To move to the top, use position 0.

Space between text and edge improves appearance

JTextArea leaves no space between the edge and text that it holds. This can be fixed by adding an empty border to it.

outArea.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));

See Borders.

Example

See Example - JTextArea.

Other

JTextArea is a child class of JTextComponent, therefore the methods of JTextComponent are available, for example, to find and set the caret position or selected text, to handle cut, copy, paste operations to/from the clipboard, and even read and write methods.

If you use a text area only to display results (no user input), use ta.setEditable(false) to prevent the user from changing it.

Ask programming questions?

 

 

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name

 

 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright 2007. All rights reserved.

[an error occurred while processing this directive]