AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Text Areas and drawString

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
BladeZ
Guest





PostPosted: Thu Feb 24, 2005 4:24 pm    Post subject: Text Areas and drawString Reply with quote



Ok I have a task to do,

"Write a program that uses drawString to display the number of characters
that the user enters into a text area and updates the count each time a
check box event occurs. If the check box has been selected then the count
should be on the piece of text that has been selected by clicking and
dragging the mouse, otherwise it should be on the whole of the text in the
text area."

I'm pretty new to Java, I've done it at a slow pace since last September.
I have a reference book "Bell and Parr Java for Students" however its
explanation of textArea is not helpful at all.

I'm really stuck on this! This is all i have so far

--------------------------------------
import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class CharacterCount extends Applet{

public void init() {

TextArea textArea = new TextArea(10, 30);
textArea.setText("place your message here");
add(textArea);
}

}//make a method for adding up selected. getselectedtextend and "start.

As you can see theres not much, I have some notation at thre bottom of how
I think I can sort out the selected text problem.

If anyone can throuw me some pointers or suggestion it'd be a huge help!

Thanks in advance.

Back to top
klynn47@comcast.net
Guest





PostPosted: Thu Feb 24, 2005 10:00 pm    Post subject: Re: Text Areas and drawString Reply with quote



As a start, you need to move the declaration of the TextArea out of the
init() method. I would suggest making it an instance variable of your
class. If you declare it within the init method, then it becomes
garbage after the init() method exits.

In order to retrieve the text from a TextArea, you call the method
getText(). You can also get the selected text with getSelectedText().

Each of these return a String and you can easily get the length of the
String.

As for using the CheckBoxes, you'll need to implement the ItemListener
interface. It conains one abstract method

public void itemStateChanged(ItemEvent e);

Within this method, you can determine whether the source of the event
was checked or unchecked by calling the method getStateChange() and
comparing it to the two constants ItemEvent.SELECTED and
ItemEvent.DESELECTED.

You can then add an instance variable of type String to your applet
called output.

Based on the whether the checkbox was selected or deselected, you can
set the value of output and then repaint.

In your paint method, use drawString to draw output.

Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.