Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2005

Tip: Looking for answers? Try searching our database.

Text Areas and drawString

Thread view: 
BladeZ - 24 Feb 2005 16:23 GMT
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 - 24 Feb 2005 21:57 GMT
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.