This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trapping keys in java applet

 
uday bhaskar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Could any one tell me in java how to trap the keys..
for example i have an textfield and i want to restrict the
length of the textfield to 10 characters. so after reaching the
10th position,cursor must not move to next character..
Help, greatly appreciated.
cheers
uday
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Uday,
You didn't specify for AWT or Swing. I have included both to get you started.
AWT approach: Won't handle cutting and pasting more chars than limit!

Swing approach: will handle cutting and pasting!

Enjoy,
Manfred.
 
uday bhaskar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Manfred.
Yaap I need for awt only!!!
 
uday bhaskar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Manfrid,
Even i got in this way!!!
Sample code like this!!!
//<applet code ="Check1.class" height =500 width =500> </applet>
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Check1 extends Applet implements KeyListener
{
public void init()
{
setLayout(new FlowLayout(FlowLayout.LEFT));
TextField ta = new TextField(30);
add(ta);
ta.addKeyListener(this);
}
public void keyTyped(KeyEvent evt){}
public void keyPressed(KeyEvent evt)
{
char c = evt.getKeyChar();
if( c == '/' | | c == '}' | | c == '{' )
{
evt.consume();
}
}
public void keyReleased(KeyEvent e1){}
};

This above code works fine for me..
 
uday bhaskar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Manfrid,
Now i am able to trap the keys!!!
Following code works fine for me!!

Cheers
uday
//<applet code ="Check1.class" height =500 width =500> </applet>
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Check1 extends Applet implements KeyListener
{
public void init()
{
setLayout(new FlowLayout(FlowLayout.LEFT));
TextField ta = new TextField(30);
add(ta);
ta.addKeyListener(this);
}
public void keyTyped(KeyEvent evt){}
public void keyPressed(KeyEvent evt)
{
char c = evt.getKeyChar();
if( c == '/' | | c == '}' | | c == '{' )
{
evt.consume();
}
}
public void keyReleased(KeyEvent e1){}
};
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic