For Programmers: Free Programming Magazines  


Home > Archive > Java Help > February 2005 > Text Areas and drawString









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Text Areas and drawString
BladeZ

2005-02-24, 4:01 pm

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.

klynn47@comcast.net

2005-02-24, 8:59 pm

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.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com