Solved

Fixed size TextArea/TextField possible?

Posted on 1997-05-10
6
467 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

773 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