 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
BladeZ Guest
|
Posted: Thu Feb 24, 2005 4:24 pm Post subject: Text Areas and drawString |
|
|
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
|
Posted: Thu Feb 24, 2005 10:00 pm Post subject: Re: Text Areas and drawString |
|
|
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 |
|
 |
|
|
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
|
|