This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Trapping keys in java applet  RSS feed

 
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
 
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){}
};
 
mooooooo ..... tiny ad ....
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!