?
Solved

Fixed size TextArea/TextField possible?

Posted on 1997-05-10
6
Medium Priority
?
490 Views
Last Modified: 2008-03-10
I'm making a series of applets which all contain input areas for dates (year, month, day). These areas should be 4,2 and 2 characters wide respectively. When I do this however, the text you write in the area just scrolls if there is more than the width of the area. Isn't it possible to make an input area with a fixed size, e.g. where you can write 2 characters and ONLY 2 characters?
0
Comment
Question by:bjoern
[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
  • 3
  • 3
6 Comments
 
LVL 6

Expert Comment

by:jpk041897
ID: 1220325
Its possible, but you are going to have to subclass either textField or textArea.

An eisier alternative would be to catch keyboard events using keyDoun(Event evt, int key), maintain a character count for each field and just append the charachter via appendText().

Which method would you be intrested in?
0
 

Author Comment

by:bjoern
ID: 1220326
I would prefer to subclass textField as this is "cleaner".
If you (jpk) could supply with a way to do this, the 50 points are yours :)
0
 
LVL 6

Accepted Solution

by:
jpk041897 earned 100 total points
ID: 1220327
This should do the trick for you:

Warning: Untested code.


public class FixedTextField extends TextField {
   int count = 0;
   int  max = 0;

   FixedTextField( String theString ) {
      super( theString );
   }
 
   FixedTextField( String theString, int MaxSize ) {
      max = MaxSize;
      super( theString );
   }

// keyDown count the input
   public boolean keyDown( Event event,int key ) {

       if (max == 0){ // if the edit cell is of unlimited size
          super.keyDown( event,key );
          return true;
      }
      if (count < max){
         if (key != 8) // If not backspace
            count++;
         else
             count--;
         auper.keyDown( event,key );
      }
      return( true );
   }

}
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:bjoern
ID: 1220328
I can see what the idea is, the only problem is that the keyDown event doesn't print the pressed key in the textfield! is super.keyDown() the right way to do this or what?

Maybe you could try it yourself. If this little problem gets solved the points are yours jpk.
0
 
LVL 6

Expert Comment

by:jpk041897
ID: 1220329
You are right, it does not!

It seems that TextField does not process its own events!?!

So we have to override event.

Here is a class that works:

import java.awt.*;

//Define a custom TextField class that can respond to events.
//  Note that this class extends TextField.
class TF extends TextField{

  int maxCount = 5;
  int count = 0;       
  TF(String inString, int MaxCount){
     setText(inString);
     maxCount = MaxCount;
  }//end constructor

  //The following event handler method handles events at the
  // component level as opposed to the container level.
  public boolean handleEvent(Event evObj){
 
    switch(evObj.id){
      case Event.KEY_PRESS :
        if (count < maxCount){
             char[] c = new char[1];
             c[0]= (char) evObj.key;
             String s = new String(c);
             setText(getText() + s);
             count++;
          }
          break;
      default : //catch and display all other event types here
          break;
    }//end switch
    return super.handleEvent(evObj);
  }//end handleEvent() in class TF
 
}//end class TF
//===================================================================

And folloing is a sample app that uses it:

import java.awt.*;


public class Event05 extends Frame{

  public Event05(){
    TF myTextField = new TF("", 5);//instantiate custom TextField object
    add ("Center",myTextField);
    myTextField.setEditable(false);//make it non-editable

    resize(300,50);//set frame size    
   }//end constructor

  //Create and show the frame object
  public static void main(String[] args){
    Event05 displayWindow = new Event05(); //instantiate obj of this type
    displayWindow.show();//display the frame
  }//end main
 
 
  public boolean handleEvent(Event evObj){
    //Terminate program if user closes the window
    if(evObj.id == Event.WINDOW_DESTROY) System.exit(0);
    return super.handleEvent(evObj);                      
  }//end handleEvent()
 
}//end class Event05



Note: Don't chage the points value. It was fun, but If the points were worth money (and not just brag points :-) ) I would have asked you to cancel my previous answer, increae the point bvalue to about 200 and then give you this answer.

Learned a bit though.
0
 

Author Comment

by:bjoern
ID: 1220330
Thanks, sorry about the additional work, but at least you learned something too :)
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month11 days, 11 hours left to enroll

752 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