?
Solved

jtextarea gettext

Posted on 2002-07-28
8
Medium Priority
?
1,579 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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 800 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month16 days, 2 hours left to enroll

850 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