TextField methods help!

I have a textField and a textArea:

TextArea messages = new TextArea(10,50);                      
TextField message = new TextField(52);

I have an ActionListener here:

        message.addActionListener(new ActionListener()
      {
            public void actionPerformed(ActionEvent e)
            {
                // Complete message sending action
               
               
               
            }
      };

What I wanna do is, when the user enter stuff into the textField and press enter, I wan the text to appear in textArea and the textField will be cleared. First, which method of TextField should I use to get the text? And second, how can I clear the field?

jtcyAsked:
Who is Participating?
 
JugglerWConnect With a Mentor Commented:
TRy this:

TextArea messages = new TextArea(10,50);                      
TextField message = new TextField(52);

I have an ActionListener here:

        message.addActionListener(new ActionListener()
     {
          public void actionPerformed(ActionEvent e)
          {
              String text = message.getText();              
              String tmp = messages.getText();
              tmp += "\r\n" + text;
              messages.setText( tmp );
              message.setText("");
          }
     };
0
 
jtcyAuthor Commented:
um~~~ does that assume messages to have only one sentence? It should be more than one sentences.....
0
 
JugglerWCommented:
What do you mean with sentence?
You may append new lines of text to the text already in text area as I've done above with.

    tmp += "\r\n" + text;

Note the += !

You may also use append() to add to end of text:

messages.append( message.getText() );

0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
jtcyAuthor Commented:
Oh thanks, it;s working
0
 
jtcyAuthor Commented:
What about I wanna have a "->" sign in front of the user's own message in messages? Like if the user enter hello, it will apeear as -> hello in the textArea?
0
 
JugglerWCommented:
JUst add the -> before you appenmd to textarea:

messages.append( "-> " + message.getText() );
0
 
jtcyAuthor Commented:
Thanks!

Um...how do i set the scrollbar to hm..sort of automatically scrolling down when there are new messages?? Like....for now, if the text area has been full of messages, the next messages wont appear unless the user scrolls the bar down himself. How can i make the scrollbar scrolls down itself when it is needed? ie. always update the latest messaage at the bottom?

0
 
JugglerWCommented:
use JTextArea.scrollRectToVisible(); ( see JDK doc for details)

And: Please open new question to get more help and to give others a chance.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.