Link to home
Start Free TrialLog in
Avatar of jtcy
jtcy

asked on

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?

ASKER CERTIFIED SOLUTION
Avatar of JugglerW
JugglerW

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jtcy
jtcy

ASKER

um~~~ does that assume messages to have only one sentence? It should be more than one sentences.....
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() );

Avatar of jtcy

ASKER

Oh thanks, it;s working
Avatar of jtcy

ASKER

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?
JUst add the -> before you appenmd to textarea:

messages.append( "-> " + message.getText() );
Avatar of jtcy

ASKER

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?

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

And: Please open new question to get more help and to give others a chance.