Solved

jtextarea gettext

Posted on 2002-07-28
8
1,522 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.

724 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