Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how can i get random sort in an array?

Status
Not open for further replies.

diljava

Programmer
Jul 19, 2003
2
IN
how can i accept only numeral with getKeyChar?
 
Try converting the value to an Integer. If it fails (ie its not a number) then it will throw NumberFormatException - you can catch this and take appropriate action.
 
If your use a JTextField to get your input you can use the following class "NumericInputField" which only accepts numeric keys and the backspace and delete keys. [Should also work for an Awt TextField].
Code:
import javax.swing.*;
import java.awt.event.*;

public class NumericInputField extends JTextField {

  public NumericInputField() {
    this.addKeyListener(new KeyAdapter() {
    public void keyTyped(KeyEvent e) {
      char c = e.getKeyChar();
      if (!((Character.isDigit(c)) ||
         (c == KeyEvent.VK_BACK_SPACE) ||
         (c == KeyEvent.VK_DELETE))) {
          getToolkit().beep();
          e.consume();
        }
      }
    });
  }

  public static void main(String args[]) {
    JFrame f = new JFrame("NumericExample");
    JTextField numField = new NumericInputField();
    f.getContentPane().add(numField);
    f.setSize(200,300);
    f.setVisible(true);
  }

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top