Solved

Fixed size TextArea/TextField possible?

Posted on 1997-05-10
6
457 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
create a gui in perl 3 70
Image decoding from Camera 3 72
diffSum example 4 23
table example 4 21
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

911 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

22 Experts available now in Live!

Get 1:1 Help Now