Solved

jtextarea gettext

Posted on 2002-07-28
8
1,485 Views
Last Modified: 2012-06-21
I have problem in get the content I fill in a JtextArea,
The followint is my code, why remark is null after I fill in some string?

      textArea.setColumns(15);
      textArea.setRows(3);
      JEditorPane editorPane = new JEditorPane();
      editorPane.add(textArea);
      JScrollPane areaScrollPane = new JScrollPane(editorPane);


      String remark = this.textArea.getText();
      System.out.println("remark = "+remark);
0
Comment
Question by:stanleyhuen
8 Comments
 
LVL 92

Expert Comment

by:objects
ID: 7184330
Has any text been entered into the text area?

can I also see all the code for the class.
0
 
LVL 9

Expert Comment

by:Ovi
ID: 7184430

      textArea.setColumns(15);
      textArea.setRows(3);
      textArea.setText("test");
      JEditorPane editorPane = new JEditorPane();
      editorPane.add(textArea);
      JScrollPane areaScrollPane = new JScrollPane(editorPane);


      String remark = this.textArea.getText();
      System.out.println("remark = "+remark);

but anyway why do you add your text area to a JEditorPane ? Why don't you just put'it in the scrollpane?
0
 

Author Comment

by:stanleyhuen
ID: 7184784
The users are allowed to add some text in the text area, so I use JEditorPane, is it correct?
0
 
LVL 92

Expert Comment

by:objects
ID: 7184791
You can simply use a JTextArea for that.
A JEditorPane is a more sophisticated text component.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 9

Expert Comment

by:Ovi
ID: 7184804
As objects said JEditorPane is a more complex text component meening that it can display and edit html content. It's subclass called JTextPane is capable editing/viewing of html, rtf, styled text.

To use just the text area modify to following:

     textArea.setColumns(15);
      textArea.setRows(3);
      textArea.setText("test");
      JScrollPane areaScrollPane = new JScrollPane(textArea);


0
 
LVL 2

Expert Comment

by:macgre
ID: 7185409
I ran a simple test to confirm my what I suspected, the string is not null it is empty there is a difference.

My test code follows:
Frame1 f = new Frame1();
JTextArea ta = new JTextArea(3, 15);
JEditorPane ep = new JEditorPane();
JScrollPane sp = new JScrollPane();

ep.add(ta);
f.getContentPane().add(sp);

String t = ta.getText();

System.out.println("Text: " + ((t==null)?"IS NULL": t));

The output was:
'Text: '
0
 
LVL 3

Expert Comment

by:yasser_helmy
ID: 7185686
The JTextArea.getText() method never returns null If the text area is empty, it returns an empty string "".
Are you sure some text is in the text area before the call to getText()? Does any thread change the text area contents?
Make sure there are no calls to setText("") before the getText();
0
 
LVL 9

Accepted Solution

by:
Ovi earned 200 total points
ID: 7187147
Try this :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test extends JFrame {
  JTextArea textArea;
  JButton put;
  JButton get;

  public test() {
    initTest();
    setListaners();
  }

  protected void initTest() {
    Container c = getContentPane();
    c.setLayout(new BorderLayout());
    textArea = new JTextArea();
    put = new JButton("Put text");
    get = new JButton("Get text");
    c.add(new JScrollPane(textArea), BorderLayout.CENTER);
    JPanel p = new JPanel();
    p.add(put);
    p.add(get);
    c.add(p, BorderLayout.SOUTH);
  }
 
  protected void setListeners() {
    put.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        textArea.append("New content inserted.\n");
      }
    });
    get.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        System.out.println("---------------------");
        System.out.println("Curent content : " + textArea.getText());
        System.out.println("---------------------");
      }
    });
  }

  public static void main(String[] args) {
    test t = new test();
    t.setSize(200, 300);
    t.setLocation(200, 100);
    t.setVisible(true);
  }
}
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now