Solved

TextField methods help!

Posted on 2004-08-13
8
277 Views
Last Modified: 2010-03-31
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
Comment
Question by:jtcy
  • 4
  • 4
8 Comments
 
LVL 7

Accepted Solution

by:
JugglerW earned 50 total points
ID: 11792483
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
 

Author Comment

by:jtcy
ID: 11792621
um~~~ does that assume messages to have only one sentence? It should be more than one sentences.....
0
 
LVL 7

Expert Comment

by:JugglerW
ID: 11792684
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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

Author Comment

by:jtcy
ID: 11792987
Oh thanks, it;s working
0
 

Author Comment

by:jtcy
ID: 11793042
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
 
LVL 7

Expert Comment

by:JugglerW
ID: 11793536
JUst add the -> before you appenmd to textarea:

messages.append( "-> " + message.getText() );
0
 

Author Comment

by:jtcy
ID: 11803811
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
 
LVL 7

Expert Comment

by:JugglerW
ID: 11804639
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

772 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