Solved

jtextarea gettext

Posted on 2002-07-28
8
1,514 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
login form jsp example 2 69
java imports not found 4 71
JDBC performance 8 33
Java array 21 148
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Suggested Courses

739 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