How to ignore the key press event in Swing?

I want to make the text field only accept numeric and backspace button from the user.

I have added the function that use to check the keycode from the user, but I don't know how to stop the key press event if the keycode is not numeric.

What code do I need to add into the function to stop the event??

 private void jTextField2KeyPressed(java.awt.event.KeyEvent evt)   

   if(!((evt.getKeyCode()==8) || (evt.getKeyCode()>48 && evt.getKeyCode()<57)))
      //how to stop the key pressed event


You don't need to mess with the Swing event handling to achieve this.

The best way is to use a JFormattedTextField instead.

See the Java tutorial for details:

Need Your Help

Build fails when compiling DLL Injector

c++ visual-studio-2010 visual-c++ dll-injection

I am a newbie C++ Coder. I've been trying to compile my own DLL Injector from an internet tutorial.

Multithreaded TableViewController

iphone ios multithreading uitableview grand-central-dispatch

I recently got a crash in UITableViewController's tableView:cellForRowAtIndexPath:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.