Solved

Fixed size TextArea/TextField possible?

Posted on 1997-05-10
6
449 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
  • 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 50 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now