Link to home
Start Free TrialLog in
Avatar of bjoern
bjoern

asked on

Fixed size TextArea/TextField possible?

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?
Avatar of jpk041897
jpk041897

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?
Avatar of bjoern

ASKER

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 :)
ASKER CERTIFIED SOLUTION
Avatar of jpk041897
jpk041897

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bjoern

ASKER

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.
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.
Avatar of bjoern

ASKER

Thanks, sorry about the additional work, but at least you learned something too :)