[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

jtextarea gettext

Posted on 2002-07-28
8
Medium Priority
?
1,568 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

649 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