Solved

TextField methods help!

Posted on 2004-08-13
8
281 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
[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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

726 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