[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 284
  • Last Modified:

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?

0
jtcy
Asked:
jtcy
  • 4
  • 4
1 Solution
 
JugglerWCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now